Spring05-DI之注解
1.byType方式注入
- 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.xsd"> <!-- bean definitions here -->
//组件扫描器
<context:component-scan base-package="com.caorui.pojo"></context:component-scan>
</beans>
- pojo层
package com.caorui.pojo;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* 与@Component具有相同功能的还有另外三个注解
* @Repository 该注解添加在Dao实现类上
* @Service 该注解添加在Service实现类上
* @Controller 该注解添加在Controller类上
*/
//当前类交给spring容器管理
@Component
@Scope("prototype") //每次获取star对象是一个新的对象
public class Star implements Serializable{
//简单数据类型通过@value注入
@Value("李四")
private String name;
@Value("23")
private int age;
//引用数据类型通过autowired注入;默认是是byType方式;如果用byName方式自动注入,则需要用@Qualifier自动注入
/*
* @Autowired
*
* @Qualifier("myPartner")
*
* @Resource 引用数据类型也可以用 默认情况用是byName方式注入,只用找不到与名称匹配的bean的时候才会按照类型进行注入
*
*/
@Autowired
// @Qualifier("myPartner")
private Partner partner;
@Override
public String toString() {
return "Star [name=" + name + ", age=" + age + ", partner=" + partner + "]";
}
}
package com.caorui.pojo;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//当前类交给spring容器管理
@Component
public class Partner implements Serializable{
@Value("caorui")
private String name;
@Override
public String toString() {
return "Partner [name=" + name + "]";
}
}
2.byName方式注入
- pojo层
package com.caorui.pojo;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* 与@Component具有相同功能的还有另外三个注解
* @Repository 该注解添加在Dao实现类上
* @Service 该注解添加在Service实现类上
* @Controller 该注解添加在Controller类上
*/
//当前类交给spring容器管理
@Component
@Scope("prototype") //每次获取star对象是一个新的对象
public class Star implements Serializable{
//简单数据类型通过@value注入
@Value("李四")
private String name;
@Value("23")
private int age;
//引用数据类型通过autowired注入;默认是是byType方式;如果用byName方式自动注入,则需要用@Qualifier自动注入
/*
* @Autowired
*
* @Qualifier("myPartner")
*
* @Resource 引用数据类型也可以用 默认情况用是byName方式注入,只用找不到与名称匹配的bean的时候才会按照类型进行注入
*
*/
@Autowired
@Qualifier("myPartner") //注意
private Partner partner;
@Override
public String toString() {
return "Star [name=" + name + ", age=" + age + ", partner=" + partner + "]";
}
}
package com.caorui.pojo;
import java.io.Serializable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//当前类交给spring容器管理
@Component("myPartner") //注意!
public class Partner implements Serializable{
@Value("caorui")
private String name;
@Override
public String toString() {
return "Partner [name=" + name + "]";
}
}
- 注意导入的包
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15(1).jar
spring-aop-4.1.6.RELEASE.jar
spring-beans-4.1.6.RELEASE.jar
spring-context-4.1.6.RELEASE.jar
spring-core-4.1.6.RELEASE.jar
spring-expression-4.1.6.RELEASE.jar