之前的学习中都是利用 XML 配置中的 bean
标签来进行 Bean 的注入,现在来学习一下如何利用注解实现 Bean 注入。
applicationContext.xml
)中配置需要扫描的注解<context:component-scan base-package="com.cunyu.pojo"/>
package com.cunyu.pojo;
import org.springframework.stereotype.Component;
/**
* @author : cunyu
* @version : 1.0
* @className : User
* @date : 2020/7/18 10:21
* @description : TODO
*/
// 相当于 XML 配置文件中的 <bean id="user" class="com.cunyu.pojo.User"/>
@Component("user")
public class User {
private String name;
public User() {
name = "村雨遥";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.cunyu.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author : cunyu
* @version : 1.0
* @className : UserTest
* @date : 2020/7/18 10:22
* @description : TODO
*/
public class UserTest {
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) applicationContext.getBean("user");
System.out.println(user.getName());
}
}
@Component
在不同的层又可以衍生出其他三个注解,其功能和 @Component
一样,只是为了更好地分层。
@Controller
:一般用在 Web 层,前端访问控制器@Service
:一般用在 Service 层,数据服务层,业务代码@Repository
:一般用在 Dao 层,数据访问成要实现属性的注入,可以用 @Value
注解,此时不需要提供 set
方法即可实现;
package com.cunyu.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author : cunyu
* @version : 1.0
* @className : User
* @date : 2020/7/18 10:21
* @description : TODO
*/
@Component("user")
public class User {
// 相当于 XML 配置中的 <property name="name" value="村雨遥"/>
@Value("村雨遥")
private String name;
public String getName() {
return name;
}
}
注意: 属性注入可以不提供 set
方法,但如果提供了 set
方法,也是可以的,此时相当于给该属性提供默认值;
package com.cunyu.pojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author : cunyu
* @version : 1.0
* @className : User
* @date : 2020/7/18 10:21
* @description : TODO
*/
@Component("user")
public class User {
private String name;
public String getName() {
return name;
}
@Value("村雨遥")
public void setName(String name) {
this.name = name;
}
}
此时可以通过 set
方法进行属性值的设置;
package com.cunyu.pojo;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author : cunyu
* @version : 1.0
* @className : UserTest
* @date : 2020/7/18 10:22
* @description : TODO
*/
public class UserTest {
@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = (User) applicationContext.getBean("user");
// 输出默认值 "村雨遥"
System.out.println(user.getName());
user.setName("小鱼");
// 输出设置后的值 "小鱼"
System.out.println(user.getName());
}
}
前面已经介绍了基于 XML 的配置和基于注解的配置,现在来了解下基于 Java 类配置。
package com.cunyu.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @author : cunyu
* @version : 1.0
* @className : User
* @date : 2020/7/18 10:21
* @description : TODO
*/
@Component
public class User {
private String name;
public String getName() {
return name;
}
@Value("村雨遥")
public void setName(String name) {
this.name = name;
}
}
package com.cunyu.config;
import com.cunyu.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author : cunyu
* @version : 1.0
* @className : UserConfig
* @date : 2020/7/18 11:14
* @description : TODO
*/
// 表明该类是一个配置类
@Configuration
public class UserConfig {
// 通过方法来注册一个 Bean,方法名对应 bean 标签中的 id,方法返回值对应 Bean 的类型
@Bean
public User user() {
return new User();
}
}
package com.cunyu.pojo;
import com.cunyu.config.UserConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
/**
* @author : cunyu
* @version : 1.0
* @className : UserTest
* @date : 2020/7/18 10:22
* @description : TODO
*/
public class UserTest {
@Test
public void test() {
ApplicationContext applicationContext = new AnnotationConfigApplicationContext(UserConfig.class);
User user = (User) applicationContext.getBean("user");
System.out.println(user.getName());
user.setName("小雨");
System.out.println(user.getName());
}
}