(一)各个注解作用
@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Repository用于标注数据访问组件,即DAO组件。
@Service用于标注业务层组件、
@Controller用于标注控制层组件(如struts中的action)
@Scope用于指定scope作用域的(用在类上),有以下范围:
1.singleto:spring IOC容器中仅有一个实例
2.prototype:每次调用bean返回一个新的实例
3.request:每次HTTP请求都会创建一个新的bean
4.session:同一个HTTP Session共享一个bean
5.global session 同一个全局session共享一个bean
6.application:同一个application共享一个bean
(重点1和2)
@Autowired 默认按类型装配
@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。使用方法类似@Autowired,有:
@Resource(name=“zhangsan”),
@Resource(type=LiSi.class);
@Qualifier指定注解bean名称
@Autowired @Qualifier(“lisi”) 存在多个实例配合使用
1.@Autowired 配合@Qualifier实例指定bean(此处我们选择李四):
public interface People {
public String getPeopleName();
}
public class ZhangSan implements People{
@Override
public String getPeopleName() {
// TODO Auto-generated method stub
return "我是张三";
}
}
public class LiSi implements People{
@Override
public String getPeopleName() {
// TODO Auto-generated method stub
return "我是李四";
}
}
public class PeopleMaker {
@Autowired
@Qualifier("lisi")
private People people;
@Override
public String toString() {
return people.getPeopleName();
}
}
public class TestMethod {
public static void main(String[] args) {
//创建spring容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取spring创建的user对象
PeopleMaker p=(PeopleMaker)ac.getBean("peoplemaker");
System.out.println(p);
}
}
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:component-scan base-package="com.spring" />
<bean id="peoplemaker" class="com.spring.model.PeopleMaker"></bean>
<bean id="zhangsan" class="com.spring.model.ZhangSan"></bean>
<bean id="lisi" class="com.spring.model.LiSi"></bean>
</beans>
2.Sevice配合Scope简化代码:
@Service
public class Monkey {
private String monkeyName="monkeyKing";
@Override
public String toString() {
return "Monkey [monkeyName=" + monkeyName + "]";
}
}
@Service
public class Tiger {
private String tigerName="tigerKing";
@Override
public String toString() {
return "Tiger [tigerName=" + tigerName + "]";
}
}
@Service("zoos")
@Scope("prototype")
public class Zoo {
@Autowired
private Tiger tiger;
@Autowired
private Monkey monkey;
@Override
public String toString() {
return "Zoo [tiger=" + tiger + ", monkey=" + monkey + "]";
}
}
public class TestMethod {
public static void main(String[] args) {
//创建spring容器对象
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
//获取spring创建的user对象
Zoo z=(Zoo)ac.getBean("zoos");
System.out.println(z);
}
}
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!--省去了三行bean -->
<context:component-scan base-package="com.spring" />
</beans>