九、依赖注入
9、1 属性依赖注入
- 依赖注入方式:手动装配 和 自动装配
手动装配:一般进行配置信息都采用手动
① 基于xml装配:构造方法、setter方法 、 p标签注入
② 基于注解装配:@autowrired @resource
自动装配:
autowire="byName"
byType:按类型装配 byName:按名称装配 constructor:构造装配, auto: 不确定装配。
9、2 构造方法
Book.java
public Book(String name, String author, double price) {
super();
this.name = name;
this.author = author;
this.price = price;
}
Student.java
public Student(String name, Book book) {
super();
this.name = name;
this.book = book;
}
xml
index :参数的索引号,从0开始 。如果只有索引,匹配到了多个构造方法时,默认使用第一个。
type :确定参数类型(下标获取可以加上这个属性 ,类型为java.lang.String……)
<!-- 方式一:通过下标获取 -->
<bean id="book" class="com.tf.constructor.Book">
<constructor-arg index="0" value="最好的我们"></constructor-arg>
<constructor-arg index="1" value="八月长安"></constructor-arg>
<constructor-arg index="2" value="123"></constructor-arg>
</bean>
<!-- 方式二:通过有参构造的name ,
普通字段用value 引用数据类型 ref
-->
<bean id="book1" class="com.tf.constructor.Book">
<constructor-arg name="name" value="何以笙箫默"></constructor-arg>
<constructor-arg name="author" value="饶雪漫"></constructor-arg>
<constructor-arg name="price" value="121"></constructor-arg>
</bean>
<!-- 创建Student对象 -->
<bean id="stu" class="com.tf.constructor.Student">
<constructor-arg name="name" value="苏苏"></constructor-arg>
<constructor-arg name="book" ref="book"></constructor-arg>
</bean>
9、3 setter方法
调用setter方法注入属性值 setName() setAuthor() setPrice()( 必须需要无参构造)
<bean id="b" class="com.tf.setter.Book">
<!--
必须需要无参构造
调用setter方法注入属性值
setName() setAuthor() setPrice()
-->
<property name="name" value="独孤天天"></property>
<property name="author" value="CC"></property>
<property name="price" value="233"></property>
</bean>
<bean id="s" class="com.tf.setter.Student">
<property name="name" value="喵喵"></property>
<property name="book" ref="b"></property>
</bean>
9、4 p命令空间注入(了解)
1、对“setter方法注入”进行简化,替换<property name="属性名">,而是在
<bean p:属性名="普通值" p:属性名-ref="引用值">
2、p命名空间使用前提,必须添加命名空间
3、代码:
<!-- p标签注入 -->
<bean id="b" class="com.tf.p.Book" p:name="笑死" p:author="李希" p:price="100"></bean>
<bean id="stu" class="com.tf.p.Student" p:name="天天" p:book-ref="b"></bean>
9、5 集合注入
MyCollection.java
private String array[];
private List<String> list;
private Set<String> set;
private Map<String,Object> map;
private Properties properties;
private List<Book> book;
spring-di.xml
<!-- 各种集合的注入 -->
<bean id="b" class="com.tf.collection.Book">
<property name="name" value="十万个为啥"></property>
<property name="author" value="大侠"></property>
<property name="price" value="233"></property>
</bean>
<bean id="myCollections" class="com.tf.collection.MyCollection">
<!-- 数组 -->
<property name="array">
<array>
<value>足球</value>
<value>篮球</value>
<value>排球</value>
</array>
</property>
<!-- list -->
<property name="list">
<list>
<value>java</value>
<value>python</value>
</list>
</property>
<!-- List<Book> -->
<property name="book">
<list>
<ref bean="b"></ref>
</list>
</property>
<!-- set -->
<property name="set">
<set>
<value>数学</value>
<value>英语</value>
</set>
</property>
<property name="map">
<map>
<entry>
<key><value>name</value></key>
<value>李四</value>
</entry>
<entry>
<key><value>age</value></key>
<value>12</value>
</entry>
</map>
</property>
<!-- properties类型 -->
<property name="properties">
<props>
<prop key="dirverClassName">
com.mysql.jdbc.Driver
</prop>
<prop key="url">
jdbc:mysql///mydb
</prop>
</props>
</property>
</bean>
十、常用注解(重点)
面试题:请说出spring常用的注解有哪些?
- 注解:就是一个类,使用@注解名称
- 开发中:使用注解 取代 xml配置文件。
①: @Component取代<bean class="">
@Component("id") 取代 <bean id="" class="">
②:web开发,提供3个@Component注解衍生注解(功能一样)取代<bean class="">
@Repository :dao层
@Service:service层
@Controller:web层
③:依赖注入,给私有字段设置,也可以给setter方法设置
方式1:按照【类型】注入
@Autowired(spring提供的)
private BookDao bookDao;
方式2:按照【名称】注入2
@Resource( name="名称") (jdk提供的,jdk>1.6版本)
@Resource( name="bookDao")
private BookDao bookDao; //spring容器中找name为bookDao
方式3:按照类型注入(autowired)时,如果有两个匹配结果,会报错
可以使用如下解决方案:
@Qualifier("userServiceImpl2")
注意:@Qualifier不能当独使用,一般配合autowired使用
④:生命周期
初始化:@PostConstruct
销毁: @PreDestroy
⑤:作用域 (类前)
@Scope("prototype") 多例
注意:
注解使用前提,导入aop的包,添加命名空间,让spring扫描含有注解类
spring-aop-4.3.5.RELEASE.jar
在配置文件中添加context标签:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd">
<!-- 需要context节点来扫描有注解的包 及其子包 -->
<context:component-scan base-package="com.tf.annotation"></context:component-scan>
</beans>
⑥:普通属性有关的注解
@value 注入属性值
1、加在成员变量上:通过反射的Field赋值(破坏对象的封装性)
@Value("梅梅") private String name; |
2、加在set方法上:通过set方法赋值
@Value("tom") public void setName(String name) { this.name = name; } |
@Value直接读取properties文件中的内容
- 配置文件
<!-- 扫描properties文件 --> <context:property-placeholder location="classpath:dbinfo.properties"/> <!—扫描一次--> <!-- 扫描带有注解的包 指定要扫描的包 --> <context:component-scan base-package="com.itqf"></context:component-scan>
|
3、在类中使用@value注解读取配置文件中的内容
@value(“${properties配置文件中的key}”)
@Value("${jdbc.driver}") private String driverClassName; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password; |
十一、Spring 整合Junit4测试
spring整合junit,为我们提供了方便的测试方式
1、导包:
spring-test-4.3.5.RELEASE.jar
junit4的包
2、创建测试类
//创建容器 @RunWith(SpringJUnit4ClassRunner.class) //指定创建容器时使用哪个配置文件 @ContextConfiguration("classpath:applicationContext.xml") public class RunWithTest { //将名为user的对象注入到u变量中 @Resource(name="person") private Person p; @Test public void testCreatePerson(){ System.out.println(p); } } |
十二、基于xml的自动装配(不建议使用,不容易阅读)
自动找到注入的对象:
autowire="byType" 按照类型
autowire="byName" 按照名字
autowire="default" 默认不装配
spring-di2.xml 代码:
<bean id="b" class="com.tf.setter.Book">
<property name="name" value="独孤九剑"></property>
<property name="author" value="见着"></property>
<property name="price" value="233"></property>
</bean>
<bean id="s" class="com.tf.setter.Student" autowire="byType">
<property name="name" value="笑死"></property>
<property name="book" ref="b"></property>
</bean>
测试:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/tf/setter/spring-di2.xml")
public class TestAuto {
@Resource
private Student s;
@Test
public void test1(){
System.out.println(s);
}
}