在配置文件中写: 在配置Bean中写:@PropertySources: 是2.基于特定功能注解 基于schema: Account: applicationContext.xml: four.properties: TestAnnotation3: test1: 通过注解方式: Account: 注释掉applicationContext.xml中的创建对象和扫描的部分: 上面那两种方式底层都是通过PropertySourcesPlaceholderConfiguer类来完成的: Account: 上面一样的测试方法: 基于Bean注解方式: applicationContext.xml:注解到包扫描和基于原始Bean标签 AppConfig10: TestAnnotation3: test2:package com.baizhiedu.four;
import org.springframework.beans.factory.annotation.Value;
import java.io.Serializable;
public class Account implements Serializable {
@Value("${id}")
private Integer id;
@Value("${name}")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
id=10
name=xiaojianren
package com.baizhiedu;
import com.baizhiedu.bean.Customer;
import com.baizhiedu.config.AppConfig1;
import com.baizhiedu.four.Account;
import com.baizhiedu.injection.UserDAO;
import com.baizhiedu.injection.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnnotation3 {
//用于测试:四维一体
@Test
public void test1(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
Account account=(Account) ctx.getBean("account");
System.out.println("account.getId="+account.getId());
System.out.println("account.getIName="+account.getName());
}
}
package com.baizhiedu.four;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component //注解不用在配置文件中创建对象了
@PropertySource("classpath:four.properties") //注解扫描不用在配置文件中书写了
public class Account implements Serializable {
@Value("${id}")
private Integer id;
@Value("${name}")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.baizhiedu.four;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.io.Serializable;
@Component //注解不用在配置文件中创建对象了
//@PropertySource("classpath:four.properties") //注解扫描不用在配置文件中书写了
public class Account implements Serializable {
@Value("${id}")
private Integer id;
@Value("${name}")
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.baizhiedu.four;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;
@Configuration //配置Bean
@ComponentScan(basePackages = "com.baizhiedu.four") //注解扫描,配置文件
public class AppConfig10 {
@Bean
public PropertySourcesPlaceholderConfigurer configurer(){
PropertySourcesPlaceholderConfigurer configurer=new PropertySourcesPlaceholderConfigurer();
//手工设置路径
configurer.setLocation(new ClassPathResource("four.properties"));
return configurer;
}
}
package com.baizhiedu;
import com.baizhiedu.bean.Customer;
import com.baizhiedu.config.AppConfig1;
import com.baizhiedu.four.Account;
import com.baizhiedu.four.AppConfig10;
import com.baizhiedu.injection.UserDAO;
import com.baizhiedu.injection.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAnnotation3 {
//用于测试:四维一体
@Test
public void test1(){
ApplicationContext ctx=new ClassPathXmlApplicationContext("/applicationContext.xml");
Account account=(Account) ctx.getBean("account");
System.out.println("account.getId="+account.getId());
System.out.println("account.getIName="+account.getName());
}
//测试基于Bean注解方式
@Test
public void test2(){
ApplicationContext ctx=new AnnotationConfigApplicationContext(AppConfig10.class);
Account account=(Account) ctx.getBean("account");
System.out.println("account.getId="+account.getId());
System.out.println("account.getIName="+account.getName());
}
}



