Spring 全注解配置 bean 和 调用 (8) @Aspect注解不生效解决办法 和 no-static 的警告解决办法

来源:互联网 发布:证明贪心算法是最优解 编辑:程序博客网 时间:2024/05/17 02:34
package com.xiuye.config.aspect;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.context.annotation.Configuration;import org.springframework.stereotype.Component;@Aspect//@Configurationpublic class AspectConfig {@Before("execution(** com.xiuye.component.*.description())")public void beforeDescription(){System.out.println("Before method:description!");}}

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.Conditional;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.EnableAspectJAutoProxy;import org.springframework.context.annotation.Import;import org.springframework.context.annotation.Profile;import org.springframework.context.annotation.PropertySource;import org.springframework.context.annotation.Scope;import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;import com.xiuye.bean.Car;import com.xiuye.bean.Student;import com.xiuye.bean.WhiteCar;import com.xiuye.component.ComponentForStudent2;import com.xiuye.config.aspect.AspectConfig;import com.xiuye.config.condition.StudentCondition;@Configuration@ComponentScan({"com.xiuye.component"/* , "com.xiuye.config.aspect" *//* * @Aspect可以生效, * 但是必须让spring能识别 * ,如@Component, * @ * Configuration等 */ })@EnableAspectJAutoProxy@Profile("dev")@PropertySource("test.properties")@Import({AspectConfig.class})/*@Aspect可以生效,相当于Configuration类作用,都是配置类*/public class BeanConfiguration1 {/** * 启用el表达式 ("${express}")解析功能,否则原样子语句注入 必须为static否则警告: * * * 警告: @Bean method BeanConfiguration1.pspc is non-static and returns an * object assignable to Spring's BeanFactoryPostProcessor interface. This * will result in a failure to process annotations such * as @Autowired, @Resource and @PostConstruct within the method's * declaring @Configuration class. Add the 'static' modifier to this method * to avoid these container lifecycle issues; see @Bean javadoc for complete * details. * * @return */@Beanpublic static PropertySourcesPlaceholderConfigurer pspc() {return new PropertySourcesPlaceholderConfigurer();}@Bean@Conditional(StudentCondition.class)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 effectpublic Student student(String name, String sex, int age, int level) {return new Student(name, sex, age, level);}@Beanpublic ComponentForStudent2 cfs2(Student s) {ComponentForStudent2 cfs2 = new ComponentForStudent2();cfs2.setStudent(s);return cfs2;}@Beanpublic Car car() {return new Car();}@Beanpublic WhiteCar wCar() {return new WhiteCar();}}

package com.xiuye.test;import javax.annotation.Resource;import org.junit.BeforeClass;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Value;import org.springframework.core.env.Environment;import org.springframework.test.context.ActiveProfiles;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.xiuye.bean.Car;import com.xiuye.bean.WhiteCar;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 {@Resourceprivate ComponentForStudent cfs;@Resourceprivate ComponentForStudent2 cfs2;@Resourceprivate Car car;@Resourceprivate Environment env;@Resourceprivate WhiteCar wcar;@BeforeClasspublic static void envConfig() {System.setProperty("test", "true");}@Testpublic void testCfs() {cfs.studentInfo();}@Testpublic void testCfs2() {cfs2.studentInfo();}@Testpublic void testCar() {this.car.configInfo();}@Testpublic void testWhiteCar() {this.wcar.configInfo();}//必须用"$" 而不是"#"@Value("${key}")private String value;@Value("${programminglang}")private String langs;@Value("#{systemProperties['key']}")//注意括号private String value2;@Value("#{systemProperties['programminglang']}")//注意括号private String langs2;@Value("#{T(System).currentTimeMillis()}")private long time;@Value("#{T(Math).random()}")private double d ;@Testpublic void testPropertiesFile() {System.out.println(env.getProperty("key"));System.out.println(env.getProperty("programminglang"));System.out.println(value);System.out.println(langs);System.out.println(langs2);System.out.println(value2);System.out.println(time);System.out.println(d);}}

十一月 12, 2016 7:38:18 下午 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 7:38:18 下午 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 7:38:18 下午 org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@12cbfa, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@cf57e3, org.springframework.test.context.support.DirtiesContextTestExecutionListener@6e6d5e, org.springframework.test.context.transaction.TransactionalTestExecutionListener@16532d6, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@196751b]十一月 12, 2016 7:38:19 下午 org.springframework.context.support.GenericApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.GenericApplicationContext@17d0578: startup date [Sat Nov 12 19:38:18 CST 2016]; root of context hierarchyActive profile := devtest := trueBefore method:description!I'm car's steer!Before method:description!I'm car's wheels!Before method:description!I'm car's engine!

0 0