您的位置:首页 > 编程语言 > Java开发

Spring 全注解配置 bean 和 调用 (2) @Profile 自适应不同的模式或环境中

2016-11-12 12:10 615 查看
package com.xiuye.config;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.Scope;

import com.xiuye.bean.Student;
import com.xiuye.component.ComponentForStudent2;

@Configuration
@ComponentScan("com.xiuye.component")
@Profile("dev")
public class BeanConfiguration1 {

@Bean
public Student student() {
return new Student("xiuye", "man", 18, 99);
}

/**
* @param name
* @param sex
* @param age
* @param level
* @return
*/
@Bean
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) // 必须原生,否则no longer has
// any effect
public Student student(String name, String sex, int age, int level) {
return new Student(name, sex, age, level);
}

@Bean
public ComponentForStudent2 cfs2(Student s) {
ComponentForStudent2 cfs2 = new ComponentForStudent2();
cfs2.setStudent(s);
return cfs2;
}

}

package com.xiuye.main;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.xiuye.bean.Student;
import com.xiuye.component.Component;
import com.xiuye.component.ComponentForStudent;
import com.xiuye.config.BeanConfiguration1;

public class Main {

public static void main(String []args) {

System.setProperty("spring.profiles.active","dev");//设置(开启)激活的开发环境   可以   不设置报错
//System.setProperty("spring.profiles.default","dev");//设置默认的开发环境  可以

ApplicationContext ac = new AnnotationConfigApplicationContext(BeanConfiguration1.class);
Student s = ac.getBean(Student.class);
System.out.println(s);
s = ac.getBean(Student.class, "Linda","Woman",22,70);
System.out.println(s);
ComponentForStudent cf = ac.getBean(ComponentForStudent.class);
cf.studentInfo();
Component c = ac.getBean(Component.class, "100","200");
System.out.println(c);
}

}

package com.xiuye.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.xiuye.component.ComponentForStudent;
import com.xiuye.component.ComponentForStudent2;
import com.xiuye.config.BeanConfiguration1;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=BeanConfiguration1.class)
@ActiveProfiles("dev")
public class TestMain {

@Resource
private ComponentForStudent cfs;
@Resource
private ComponentForStudent2 cfs2;

@Test
public void testCfs(){
cfs.studentInfo();
}
@Test
public void testCfs2(){
cfs2.studentInfo();
}

}

结果:

十一月 12, 2016 12:04:38 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1af6ecc: startup date [Sat Nov 12 12:04:38 CST 2016]; root of context hierarchy
Student [name=xiuye, sex=man, age=18, level=99]
Student [name=Linda, sex=Woman, age=22, level=70]
Student [name=xiuye, sex=man, age=18, level=99]
Component [_1=100, _2=200]

十一月 12, 2016 12:10:19 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getDefaultTestExecutionListenerClassNames
信息: Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
十一月 12, 2016 12:10:19 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper instantiateListeners
信息: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the default listener classes (and their required dependencies) available. Offending class: [javax/servlet/ServletContext]
十一月 12, 2016 12:10:19 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@75222b, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@203ea1, org.springframework.test.context.support.DirtiesContextTestExecutionListener@7f674d, org.springframework.test.context.transaction.TransactionalTestExecutionListener@1251891, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@140564]
十一月 12, 2016 12:10:19 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.GenericApplicationContext@109b8da: startup date [Sat Nov 12 12:10:19 CST 2016]; root of context hierarchy
Student [name=xiuye, sex=man, age=18, level=99]
Student [name=xiuye, sex=man, age=18, level=99]
十一月 12, 2016 12:10:20 下午 org.springframework.context.support.GenericApplicationContext doClose
信息: Closing org.springframework.context.support.GenericApplicationContext@109b8da: startup date [Sat Nov 12 12:10:19 CST 2016]; root of context hierarchy
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: