目录
四、@Scope("prototype") 多例、单例的配置(默认singleton单例)
首先简单说明一下什么是注解注入,回顾之前我们的案例,不管是装配对象(Bean),还是注入对象(Bean)都需要在xml配置文件中写大量的
<!--配置一个bean对象--> <bean id="userService" class="com.xyl.service.UserServiceImpl"> <property name="name" value="xyl"></property> </bean>
这种配置,那么如果我们使用注解注入,xml文件中就不需要再重复配置这些bean了,直接几行代码就搞定,想想就舒服多了。注解的书写通常是 @xxxx 写在属性、方法、类上面。
下面开始介绍常用的spring注解
一、@Component
泛指各种组件,就是说当我们的类不属于各种归类的时候(不属于action,service,dao),我们就可以使用@Component来标注这个类。
下面我们来举例具体用途。
首先创建一个演示类 Demonstration.class,里面就只有一个添加方法
package com.xyl.spring.test;
public class Demonstration {
public void add(){
System.out.println("这是一个添加方法");
}
}
如果我们之前不使用注解的时候,xml配置文件就得写一个
<bean id="demonstration" class="com.xyl.spring.test.Demonstration"/>
如果我们又要获取另一个对象xxx,就得又配置一个bean,然后各种+++
现在使用@Component就相当于我们在xml配置文件中配置了
@Component 相当于:
<bean class="com.xyl.spring.test.Demonstration"/> (没有id)
@Component("demonstration") 相当于:
<bean id="demonstration" class="com.xyl.spring.test.Demonstration"/> (有id)
下面使用注解@Component
在Demonstration类中加上注解
package com.xyl.spring.test;
@Component
public class Demonstration {
public void add(){
System.out.println("这是一个添加方法");
}
}
然后在测试代码中获取Demonstration对象,调用add方法
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("beans-info.xml");
Demonstration demonstration = context.getBean(Demonstration.class);
demonstration.add();
}
注意:现在注解还不起作用。最后我们需要修改一下beans-info.xml配置文件
1.添加beans属性:
xmlns:context="http://www.springframework.org/schema/context"
2.修改beans属性
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"
3.开启注解,设置扫描注解地址
<!--开启注解--> <context:annotation-config/> <!--注解的位置--> <context:component-scan base-package="com.xyl.spring"/>
beans-info.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">
<!--开启注解-->
<context:annotation-config/>
<!--注解的位置-->
<context:component-scan base-package="com.xyl.spring"/>
</beans>
运行结果:
配置完后,以后我们使用对象就可以直接添加注解获取就可以了,不用再去修改xml文件
二、3个@Component注解衍生注解
@Repository(“名称”):dao层
@Service(“名称”):service层
@Controller(“名称”):web层
功能一样是取代<beans class=" " />,但是与Component注解不同的是,这三个衍生注解是标注在特定的分层;
举个栗子:
我们平时页面调用action方法程序的流程是 (web)action——>service——>dao
如果所有层都用Component注解,那么spring容器将不知道先创建哪一层的对象。所以需要这三个衍生注解来区分
结构如图,那么三个衍生标签的使用如下
用不同注解注释不同层;
三、@Autowired:自动根据类型注入
这里我们申明一个对象,但是不实例化对象。然后在声明对象上面加上@Autowired注解,spring容器将自动帮我们根据类型实例化注入。
举个栗子:我们就拿action中调用service方法举例
那么,我们只要在userService上面加上@Autowired注解,spring容器就会自己去帮我们找到UserService这个对象并帮我们实例化
四、@Scope("prototype") 多例、单例的配置(默认singleton单例)
被@Scope("prototype")注解标注的对象会被设置成多例模式,每次调用都将 new一个新对象
默认是@Scope("singleton")单例模式,默认需要使用该注解