Spring的bean管理(注解)
1. 什么是注解
代码里面的一些特殊的标记。我们使用注解也可以直接来完成一些相关的功能。
注解的写法是@注解名称(属性名称=属性值)
注解可以用在类、方法、属性。
2. Spring注解开发的基本的准备工作
注解可以替代配置文件,但是不能完全脱离配置文件,只是能够减少配置。
导入jar包,就是导入spring最基本的几个jar包。
导入aop的jar包,为了使用注解。
创建一个类,在类当中,创建一个方法
创建spring的配置文件,在配置文件当中引入约束
<?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 -->
</beans>
- 开启注解扫描
<?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="cn.itcast.anno,cn.itcast.xmlanno"></context:component-scan>
<context:component-scan base-package="cn.itcast"></context:component-scan>
<context:component-scan base-package="cn"></context:component-scan>
</beans>
- 仅仅开启注解扫描当中的属性的注解扫描
<?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="cn.itcast.anno,cn.itcast.xmlanno"></context:component-scan>
<context:component-scan base-package="cn.itcast"></context:component-scan>
<context:component-scan base-package="cn"></context:component-scan>
<!--
开启属性上面的扫描
-->
<context:annotation-config></context:annotation-config>
</beans>
2.1 注解创建对象
在创建对象的类上面使用注解就可以实现。
创建对象有四个注解。
@Conmponent
@Controller web层
@Service 业务层
@Repository 持久层
目前来说,这四个注解的功能都是一样的,为什么四个注解,就是为了后续的版本当中进行功能扩展的。
3.创建对象是单实例还是双实例
类的代码
package cn.itcast.anno;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.*;
@Service(value="user") //<bean id="user" class="cn.itcast.anno.User">
@Scope(value="prototype")
public class User {
public void add(){
System.out.println("add..............");
}
}
配置文件
<?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="cn"></context:component-scan>
</beans>
测试代码
package cn.itcast.anno;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnno {
@Test
public void testUser(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
User user=(User)context.getBean("user");
System.out.println(user);
user.add();
}
}
测试结果