1.Autowired (Spring规范提供)&& Qualifier
1.可以让spring自动的把属性需要的对象找出来,并注入到对象
2.可以贴在字段或者setter方法上面
3.可以同时注入多个对象
4.可以注入一些Spring内置的重要对象,比如beanfactory,applicationcontext,servletcontext等
5.默认情况下Autowired注释必须要能找到对应的对象,否者报错。通过required = false来避免这个问题
@Autowired(required = false)
6.第三方程序:在spring3.0之前,需要手动配置Autowired注释的解析器:<context:annotation-config/> ,在web开发中必须配置
7.Autowired 注解寻找bean的方式
1)首先按照依赖对象的类型找,如果找到,就用setter或者字段直接注入
2)如果在spring上下文中寻找多个匹配的类型,再按照名字去找。如果没有匹配就报错
3)可以通过使用@qualifier("id名")标签来规定依赖对象按照bean的id和类型的组合方式去找;
代码演示
cat类
package com.keen.di;
public class Cat {
}
person类
package com.keen.di;
import org.springframework.beans.factory.annotation.Autowired;
public class Person {
@Autowired //这里使用autowired的注释就可以省去写setter方法,使用注释注入
private Cat cat;
@Override
public String toString() {
return "Person [cat=" + cat + "]";
}
}
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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- di注解解析器 -->
<context:annotation-config/>
<bean id = "cat" class = "com.keen.di.Cat"/>
<!-- 此时 就不用再用property属性配置person中的cat了-->
<bean id = "person" class = "com.keen.di.Person"/>
</beans>
test
package com.keen.di;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@SpringJUnitConfig
public class AutoTest {
@Autowired
private Person person;
@Test
void test() throws Exception {
System.out.println(person);
}
}
2.Resource(javaEE规范,其实作用和Autowired一样,看你喜欢用哪个而已)
1.也需要配置DI注释解析器<context:annotation-config/>
2.recource注解必须要能找到对应的对象,否则报错
3.recurce注释寻找bean的时候,是向按照名字去找,再按照类型去找,但如果找到多个配置类型,报错
4.可以直接使用name属性指定bean的名称(@resource (name = ""))但是,如果指定name 就按照name去找,如果找不到就按照类型去找
3.Value
Autowired&&Resource注解用于注入对象,Value注解用于注入常量数据(简单类型数据)
@Value(${server.port}")
private int port
引入配置文件
<!-- 加载property文件 -->
<context:property-placeholder location ="classpath:server.properties"/>
注意:当加载多个配置文件时:可用逗号隔开
<context:property-placeholder location ="classpath:server1.properties,classpath:server2.properties"/>
或者:设置属性:ignore-unresolvable="true"
<context:property-placeholder location ="classpath:server1.properties" ignore-unresolvable="true"/>
<context:property-placeholder location ="classpath:server2.properties" ignore-unresolvable="true"/>
具体代码
ValueBean类
package com.keen.di;
import org.springframework.beans.factory.annotation.Value;
public class ValueBean {
//@Value ("9999")
@Value("${server.port}")
private int port ;
@Override
public String toString() {
return "ValueBean [port=" + port + "]";
}
}
server.property文件
#key = value
server.port = 8888
xml配置文件
...
<!-- 加载property文件 当加载多个文件时用逗号隔开 或者设置属性ignore-unresolvable="true" -->
<context:property-placeholder location ="classpath:server.properties" ignore-unresolvable="true"/>
<bean id ="valuebean" class = "com.keen.di.ValueBean"/>
...
4.bean组件板型:四个组件的功能时相同的,只是用于标志不同类型的组件
1.Component 泛指组件,但组件不好归类的时候,我们可以用这个注释进行标注
2.Repository 用于标注数据访问的组件,即DAO 组件
3.Service 用于标注业务层组件
4.Controller 用于标注控制层组件(比如structs中的Action,springMVC的Contraller)
注意⚠️:此时需要配置Ioc注释的解析器
使用:
<!--IOC注解解析器 -->
<context:component-scan base-package=" "/>
其实说白了,就是把类交给组件注释管理
<!--IOC注解解析器 -->
<context:component-scan base-package="com.keen.ioc"/>
等同于:
//xml配置: <bean id = "myDatasource" class = "com.keen.ioc.MyDataSource"/>
5.初始化和销毁注解 和 scope注解
1.postConstruct用于贴在初始化方法上面
2.preDestory用于贴在销毁方法前面
代码
package com.keen.lifecycle;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("singleton")//默认是单例的
public class SomeBean {
public SomeBean() {
System.out.println("constuctor");
}
@PostConstruct//构造对象之后
public void open() {
System.out.println("初始化方法");
}
@PreDestroy//销毁之前
public void close () {
System.out.println("销毁方法");
}
public void doWork() {
System.out.println("working");
}
}
//其实上面注解的配置作用和下面在xml配置文件代码是一样的效果
<bean id = "someBean" class ="com.keen.lifecycle.SomeBean" scope ="singleton"
init-method="open" destroy-method="close"/>
此时我们在Xml配置文件中只需要配置解析器就可以了
<!--IOC注解解析器 -->
<context:component-scan base-package="com.keen.lifecycle"/>
网友评论