注解开发
1. 使用注解注入属性
Spring会在上下文自动寻找,自动给Bean装配属性。
在Spring中有三种装配方式:
- 在xml中显示的配置。
- 在java中显示的配置。
- 隐式的自动装配Bean。
1.1 使用标签的autowire属性
环境:
public class People {
private Cat cat;
private Dog dog;
private String name;
配置:
<bean id="cat" class="com.wu.pojo.Cat"/>
<bean id="dog" class="com.wu.pojo.Dog"/>
<bean id="people" class="com.wu.pojo.People" autowire="byType">
<property name="name" value="wu"/>
属性有两种:
byName:会自动在容器上下文中查找,是根据对象的set方法后面的值匹配bean id(所以使用这个时id值一定要和它set方法的setxxx xxx名字相等。)
byType:是根据class属性内的值来查找内容,此时id属性甚至可以不写,只要保证class属性写正确并且唯一(一个类型只能出现一次即可)
1.2 使用注解配置
- 导入注解的xml配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
- 将bean标签清理干净
<bean id="cat" class="com.wu.pojo.Cat"/>
<bean id="dog" class="com.wu.pojo.Dog"/>
<bean id="people" class="com.wu.pojo.People"/>
1.2.1 使用@Autowired注解
在实体类中添加注解
可以在属性上面添加,也可以在set方法上面添加。
public class People {
@Autowired
private Cat cat;
@Autowired
private Dog dog;
private String name;
注:我们发现当关于cat的bean不存在或者不存在与其set方法后面值相同的id时,如:
<bean id="dog" class="com.wu.pojo.Dog"/>
<bean id="people" class="com.wu.pojo.People"/>
<bean id="cat2" class="com.wu.pojo.Cat"/>
<bean id="cat233" class="com.wu.pojo.Cat"/>
<bean id="dog" class="com.wu.pojo.Dog"/>
<bean id="people" class="com.wu.pojo.People"/>
会报错
对于第二种情况我们可以使用@Qualifier一起使用,这个标签相当于指定id的值
@Autowired
@Qualifier(value = "cat233")
private Cat cat;
1.2.2 使用@Resource注解
@Resource(name = "cat233")
private Cat cat;
1.2.3 区别
@Autowired:相当于先进行autowire=“byType”,如果发现有多个相同的type就进行byName。
@Resource:类似于使用byName查找。(则时候就是固定的按照id查找了)
2. 使用注解注册Bean
- 添加配置:
context:component-scan:添加了这个配置之后,spring就会去扫描指定地址的包内的java文件,在遇到了指定的注解之后就会自动将类注册成Bean。
<context:component-scan base-package="com.wu"/>
<context:annotation-config/>
- 配置注解:
@Component:配置文件中包扫描时指定的注解,其中value参数相当于设置其id值,并且其作用在类上!
与其相对应的@Service(service层专用),@Repository(dao层专用),@Controller(Controller层专用)。
(他们的含义与@Component,只是相同按照了MVC架构分层)
一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一一
@Scope:设置作用域
@Value(“www”):为属性设置值,注意其作用于属性上!
@Component(value="user")
@Scope("singleton")
public class Userdao {
@Value("www")
public String name;
}
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
Userdao name = context.getBean("user", Userdao.class);
System.out.println(name.name);
}
3.完全的注解开发
在SpringBoot中会发现很多源码都是这么写的,不使用xml配置文件,只使用java语言和注解。
1.pojo类
@Component
public class people {
private String name;
public String getName() {
return name;
}
@Value("ww")
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "people{" +
"name='" + name + '\'' +
'}';
}
}
- 新建一个config文件夹,创建一个config的java文件
@Configuration注解,它本质上就是一个@Component
@Bean(“people”)就相当于xml文件内的bean标签,id = people,返回值就是class标签的值
@Configuration
public class wuConfig {
@Bean("people")
public people getpeople(){
return new people();
}
}
- 测试
注 写法不同
public void test(){
ApplicationContext context = new AnnotationConfigApplicationContext(wuConfig.class);
people getpeople = context.getBean("people", people.class);
System.out.println(getpeople.getName());
}
总结:我们目前使用spring的最大简化就是不用在test内新建对象,这种只使用java和注解完成 的就是在内部自己创建对象。