springday03-go1

springday02项目下新建包annotation1
1.复制xml文件到包annotation1下,并添加组件扫描方式代码
2.Waiter类实现序列化接口,构造函数,并使用特定注解标记waiter这个bean的id
3.Bar类中waiter为其成员变量,有参和无参构造器,get/set方法,以及toString方法
4.特定注解标记Bar这个bean的id,在构造器前面使用@Autowired和@Qualifier
5.test1中测试这种注解方式可以实现依赖注入
6.Bar2类与Bar的区别就是,将@Autowired和@Qualifier写在属性前,这种注入方式不会去调用set方法
7.test2测试Bar2
8.Bar3与Bar的区别就是,在set方法前使用@Resource(name="wt"),同样,也可以写在属性前
9.test3对Bar3进行测试
10.Student类添加pageSize和name属性,实现无参构造器,set/get方法,toString方法
11.使用Component注解,设定bean的id为stu
12.新建config.properties文件
13.修改xml文件,通过设定id和location属性,采用集合的方式读取properties文件。
14.为sdu的属性设置基本值,即使用spring表达式
15.test4对Student类进行测试

Waiter.java:

package container.annotation1;

import java.io.Serializable;

import org.springframework.stereotype.Component;

@Component("wt") //此bean的id为wt,若不写,则为类名的小写
public class Waiter implements Serializable {

public Waiter() {
System.out.println("Waiter的构造器");
}

}

Bar.java:

package container.annotation1;

import java.io.Serializable;

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

@Component("bar")
public class Bar implements Serializable {

private Waiter wt;

public Bar() {
System.out.println("Bar的无参构造器");
}

@Autowired //此标注加在构造器前面
//@Qualifier中的参数是要注入的bean的id,而在Waiter中我们
//已经用Component命名为wt
public Bar(@Qualifier("wt") Waiter wt) {
System.out.println("Bar的带参构造器");
this.wt = wt;
}

@Override
public String toString() {
return "Bar [wt=" + wt + "]";
}


}

Bar2.java:

package container.annotation1;

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

@Component("bar2")
public class Bar2 {

@Autowired //表示set方式注入,如果这样写,就不用再写set/get方法
@Qualifier("wt") //表示注入的参数bean为wt
private Waiter wt;

public Bar2() {
System.out.println("Bar2的无参构造器");
}



public Waiter getWt() {
return wt;
}

// @Autowired
// public void setWt(@Qualifier("wt") Waiter wt) {
// System.out.println("set方式注入");
// this.wt = wt;
// }

@Override
public String toString() {
return "Bar2 [wt=" + wt + "]";
}


}

 

Bar3.java:

package container.annotation1;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

@Component("bar3")
public class Bar3 {
@Resource(name="wt") //如果写在这里,是采用set方式注入,只是单纯的赋值语句,但它不会调用set这个方法,
//如果是写在set方法前面,则会调用set方法
private Waiter wt;

public Bar3() {
System.out.println("Bar3的无参构造器");
}

public Waiter getWt() {
return wt;
}

// @Resource(name="wt") //如果不写name,那么容器中可能会出现多个Waiter的bean,
// //建议写上name,name指定要注入的bean的id
// public void setWt(Waiter wt) {
// System.out.println("set方式注入");
// this.wt = wt;
// }

@Override
public String toString() {
return "Bar3 [wt=" + wt + "]";
}



}

 

student.java:

package container.annotation1;

import java.io.Serializable;

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

@Component("stu")
public class Student implements Serializable{

@Value("#{jdbc.pageSize}")
private String pageSize;
@Value("李白")
private String name;

public Student() {
System.out.println("Student的无参构造器方法");
}

public String getName() {
return name;
}

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

 

public String getPageSize() {
return pageSize;
}

public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}

@Override
public String toString() {
return "Student [pageSize=" + pageSize + ", name=" + name + "]";
}



}

xml:

<context:component-scan
base-package="container.annotation1"/>

<util:properties id="jdbc"
location="classpath:container/annotation1/config.properties"/>

config.properties:

pageSize=10

 

TestCase.java:

package container.annotation1;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import container.annotation.Waiter;

public class TestCase {
@Test
public void test1(){
String cfg = "container/annotation1/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
// Waiter wt = ac.getBean("wt",Waiter.class);
// System.out.println(wt);

Bar bar = ac.getBean("bar",Bar.class);
System.out.println(bar);
}


@Test
public void test2(){
String cfg = "container/annotation1/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
Bar2 bar2 = ac.getBean("bar2",Bar2.class);
System.out.println(bar2);
//因为是单例,组件扫描会对所有的bean预先实例化,,会输出前两项
}

 


@Test
public void test3(){
String cfg = "container/annotation1/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
Bar3 bar3 = ac.getBean("bar3",Bar3.class);
System.out.println(bar3);
//因为是单例,组件扫描会对所有的bean预先实例化,,会输出前两项
}

 


@Test
public void test4(){
String cfg = "container/annotation1/applicationContext.xml";
ApplicationContext ac =
new ClassPathXmlApplicationContext(cfg);
Student stu = ac.getBean("stu",Student.class);
System.out.println(stu);
//因为是单例,组件扫描会对所有的bean预先实例化,,会输出前两项
}

 


}

 

posted @ 2016-08-14 15:53  知我者,足以  阅读(148)  评论(0编辑  收藏  举报