Spring注解Annotation(二)

IoC容器装配Bean(注解配置)

使用注解配置Bean
Spring注解开发需要jar包 和 xml开发 一样的 !
第一步: 新建项目, 导入jar包
第二步: 在需要spring创建对象类上面 添加@Component (注解 来自spring2.5 )

// xml 配置 <bean id="" class="" />
// @Component("userService")当中 userService 相当于bean id属性
// @Component("userService")
@Service("userService")
public class UserService {
}

第三步: 配置注解Bean所在package
引入context命名空间

<!-- 在配置文件 引入约束 xsd schema 约束 -->
<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">
    <!-- 通知spring容器 ,注解Bean所在位置  -->
    <context:component-scan base-package="cn.itcast.spring" />
</beans>

Spring为了细分组件的功能,在提供@Component注解,提供三个等价的注解
@Controller 控制器,表示web层组件 —- action
@Service 业务类,表示业务层组件 — service
@Repository 表示持久层的组件 — dao

Bean属性依赖注入
在spring2.5 版本,没有提供基本类型属性注入 ,但是spring3.0引入注解@Value

向对象注入基本类型属性

@Component("employee")
public class Employee {
    @Value("10002")
    private int id;
    @Value("李四")
    private String name;

向对象注入复杂对象属性

@Component("employee")
public class Employee {
    @Value("10002")
    private int id;
    @Value("李四")
    private String name;

    // 方案一: spring3.0 @value 支持spEL
    // @Value("#{car}")

spring2.5 注入复杂对象属性
spring2.5 提供自动装配注解 @Autowired
@Autowired 默认按照类型进行注入 (缺点: 如果容器中存在两个对象,是相同类型, @Autowired 无法注入 )
@Autowired + @Qualifier 指定注入Bean的id

// 方案二: spring2.5 使用@Autowired 自动装配
    // @Autowired
    // @Qualifier("car")
    private Car car;

Spring支持JSR-250规范,引入@Resource ,功能和@Autowired 相同

// 方案三: JSR-250 @Resource注解等价于 @Autowired
    @Resource(name = "car")
    private Car car;

重点记忆: @Autowired 和 @Resource

初始化和销毁方法配置
@PostConstruct 初始化
@PreDestroy 销毁

@Repository("productDAO")
// 通过scope属性指定bean作用域
@Scope("prototype")
public class ProductDAO {

    // 指定初始化和销毁方法 xml 提供 init-method 、destroy-method属性
    // 无参数、返回值、非静态方法

    @PostConstruct
    // 初始化
    public void setup() {
        System.out.println("productDAO 初始化...");
    }

    @PreDestroy
    // 销毁
    public void tearDown() {
        System.out.println("productDAO 销毁...");
    }
}

注解和配置文件混合使用
Bean定义配置,使用配置文件方式
Bean关系依赖注入,使用注解配置方式
如果在配置使用 context:component-scan 具有 context:annotation-config 效果

如果Bean是通过xml配置,想使用注解依赖注入,必须单独配置

anno

web开发中应用Spring框架

直接在项目集成spring ,在servlet 创建Spring容器

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1、 创建工厂
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml")
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

        // 2、获取Bean
        HelloService helloService = (HelloService) applicationContext.getBean("helloService");
        // 3、调用业务方法
        helloService.sayHello();
    }

问题: 每次请求都会创建一个spring容器对象,将容器管理对象都重新创建
—- 性能问题、 内存溢出

解决: 在一个web容器中, 保证spring容器只被初始化一次
1) static — 问题: 违背面向对象
2) servlet init 方法中 —- 问题: spring容器对象如何被其它Servlet访问
3) ServletContext 容器保存数据唯一, ServletContextListener 初始化时容器方法只被调用一次

Spring 提供 web.jar 提供类似Listener
第一步: 在项目导入 spring-web-3.2.0.RELEASE.jar
第二步: 在web.xml 配置监听器

<!-- 监听器,初始化Spring容器对象 -->
  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

默认加载 /WEB-INF/applicationContext.xml
第三步: 配置spring文件位置

<!-- 修改spring配置文件位置 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

第四步:修改Servlet代码

// 1、 创建工厂
        ApplicationContext applicationContext = 
(ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    上面代码也可以写为 
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

spring和junit的测试集成
传统测试

@Test
    public void testDemo1() {
        // 传统测试方式
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloService helloService = (HelloService) applicationContext.getBean("helloService");
        helloService.sayHello();
    }

Spring和junit测试集成
第一步: 在项目导入 spring-test-3.2.0.RELEASE.jar
第二步: 编写测试类

@RunWith(SpringJUnit4ClassRunner.class)
// 整合spring和junit
@ContextConfiguration(locations = "classpath:applicationContext.xml")
// 指定spring配置文件位置
public class HelloServiceTest2 {
    // 测试哪个对象,进行注入
    @Autowired
    private HelloService helloService;

    @Test
    public void testDemo1() {
        // 测试代码功能
        helloService.sayHello();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值