spring(二)---bean的学习下篇

来源:互联网 发布:淘宝网镂空针织衫 编辑:程序博客网 时间:2024/06/17 18:38

一、IOC容器装配bean(xml配置)

1、通知spring容器,注解bean所在的位置,创建bean实例

<?xml version="1.0" encoding="UTF-8"?>
<!-- 在配置文件 引入约束 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="top.einino" />
</beans>

2、bean的属性注解依赖注入

a、简单数据类型属性注入

b、复杂对象类型属性注入

package top.einino.annotation;

import org.springframework.stereotype.Component;

@Component
public class Car {

}



package top.einino.annotation;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("person")
public class Person {

@Value("郑先生")
private String name;
@Value("2")
private int age;

//注入复杂对象,spel方式注入
@Value("#{car}")
private Car car;

@Autowired//自动装配
@Qualifier("car")//指定装配
private Car car1;

@Resource(name="car")
private Car car2;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public Car getCar() {
return car;
}

public void setCar(Car car) {
this.car = car;
}

public Car getCar1() {
return car1;
}

public void setCar1(Car car1) {
this.car1 = car1;
}

public Car getCar2() {
return car2;
}

public void setCar2(Car car2) {
this.car2 = car2;
}

}

c、测试

package top.einino.junit;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import top.einino.annotation.Person;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class TestAnnotation {

@Autowired
Person person;
@Test
public void testPerson(){

//测试属性注入
System.out.println(person.getName());
System.out.println(person.getAge());
//测试spel方法注入复杂属性
System.out.println(person.getCar());
//测试@Autowired方法注入复杂属性
System.out.println(person.getCar1());
//测试@Resource方法注入复杂属性
System.out.println(person.getCar2());

}

}

这里的测试采用了spring-text的jar与junit的jar包结合的方式,使得在做单元测试时更加方便!

3、bean的作用域

只说两种:

a、Singleton

package top.einino.scope;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

@Repository("blogSingleton")
@Scope("singleton")
public class BlogSingleton {

}

b、Prototype

package top.einino.scope;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

@Repository("blogPrototype")
@Scope("prototype")
public class BlogPrototype {

}

c、测试
@Resource(name="blogPrototype")
BlogPrototype blogPrototype1;
@Resource(name="blogPrototype")
BlogPrototype blogPrototype2;
@Resource(name="blogSingleton")
BlogSingleton blogSingleton1;
@Resource(name="blogSingleton")
BlogSingleton blogSingleton2;

@Test
public void testScope(){
System.out.println(blogPrototype1==blogPrototype2);//false
System.out.println(blogSingleton1==blogSingleton1);//true
}

4、bean的生命周期:初始化和销毁方法

package top.einino.life;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import org.springframework.stereotype.Component;

@Component
public class BlogLife {

public BlogLife(){
System.out.println("Blog构造");
}

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

@PreDestroy
public void teardown(){
System.out.println("Blog销毁");
}
}

测试:

@Test
public void testLife(){

}


二、小结

该博文主要介绍了spring的bean属性依赖注入,作用域、生命周期的注解方式,以及与junit整合的单元测试。


如果有疑问或者对该博文有何看法或建议或有问题的,欢迎评论,恳请指正!

0 0
原创粉丝点击