gpt4 book ai didi

Spring 3.1 Java 配置 - @Autowired、@Configuration 和@Profile 挑战

转载 作者:行者123 更新时间:2023-12-02 02:22:59 27 4
gpt4 key购买 nike

我正在尝试使用 Spring 3.1 和 Servlet 3.0,并且对新的 Java 配置选项以及使用 Spring 配置文件非常感兴趣,并且遇到了挑战。尽管我在网上看到的所有示例都使它看起来像一个用 @Configuration 注释的类可以将其他对象 @Autowired 添加到其中,但我看到的是 Autowiring 直到在一些 bean 之后才会发生@Configuration 类已经生成。

这是我的初始化程序:

public class SpringMvcInitializer implements WebApplicationInitializer {

private static org.apache.log4j.Logger log= Logger.getLogger(SpringMvcInitializer.class);

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.scan("org.jc.config");
servletContext.addListener(new ContextLoaderListener(rootContext));

// Secures the application
servletContext.addFilter("securityFilter", new DelegatingFilterProxy("springSecurityFilterChain"))
.addMappingForUrlPatterns(null, false, "/*");

ServletRegistration.Dynamic appServlet =
servletContext.addServlet("appServlet", new DispatcherServlet(new GenericWebApplicationContext()));
appServlet.setLoadOnStartup(1);

appServlet.addMapping("/");

log.info("Mvc Initializer starting");
}

我的网络配置

@Configuration
@EnableWebMvc
@ComponentScan(basePackages="org.jc" )
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public InternalResourceViewResolver configureInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".jsp");
return resolver;
}

@Override
public void configureResourceHandling(ResourceConfigurer configurer) {
configurer.addPathMapping("/resources/**");
configurer.addResourceLocation("/resources/");
}

我的 JPA 配置类

@Configuration
public class JpaConfig {
private static Logger logger = Logger.getLogger(JpaConfig.class);

private AppEnvironmentI appEnvironment;

@Autowired
public void setAppEnvironment(AppEnvironmentI appEnvironment) {
this.appEnvironment = appEnvironment;
checkAppEnv("setAppEnvironment");
}

@Bean
public AppUser globalUser(){
checkAppEnv("globalUser entry");
AppUser appUser = new AppUser();
checkAppEnv("globalUser exit");
return appUser;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
checkAppEnv("entityManagerFactory entry");
LocalContainerEntityManagerFactoryBean emf = new LocalContainerEntityManagerFactoryBean ();
emf.setDataSource(dataSource());
emf.setJpaVendorAdapter(this.jpaAdapter());
emf.setPersistenceUnitName("JcEntities2");
checkAppEnv("entityManagerFactory exit");
return emf;
}

@Bean
public DataSource dataSource(){
checkAppEnv("dataSource entry");
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName("org.apache.derby.jdbc.EmbeddedDriver");
ds.setUrl("jdbc:derby://localhost:1527/JcTestDb");
ds.setUsername("jc");
ds.setPassword("pwd");
checkAppEnv("dataSource exit");
return ds;
}

@Bean
public JpaVendorAdapter jpaAdapter() {
checkAppEnv("jpaAdapter entry");
HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
hibernateJpaVendorAdapter.setShowSql(true);
hibernateJpaVendorAdapter.setDatabase(Database.DERBY);
checkAppEnv("jpaAdapter exit");
return hibernateJpaVendorAdapter;
}

@Bean
public PlatformTransactionManager transactionManager() {
checkAppEnv("transactionManager entry and exit");
return new JpaTransactionManager( entityManagerFactory().getObject() );
}

private void checkAppEnv(String _method){
if(this.appEnvironment == null){
logger.info(_method + " - App Environment is null!!!!");
}else{
logger.info(_method + " - App Environment JpaConfig = " + appEnvironment.externalPropertiesFile().getPropertyValue("environment"));
}
}

我的文件类

@Configuration
@Profile("local")
public class AppEnvironmentLocal implements AppEnvironmentI{
private static Logger logger = Logger.getLogger(AppEnvironmentLocal.class);

@Bean
public AppEnvironment externalPropertiesFile(){
logger.info("Fetching properties file for LOCAL environment");
Properties props = FileUtilsJc.getPropertiesFromFlatFile("C:\\AppConf\\JcConf\\JcConfLocal.properties");
AppEnvironment exf = new AppEnvironment(props);
return exf;
}
}

Logger 输出:看起来 Spring 3.1 在将 AppEnvironment 注入(inject)类之前首先尝试创建 Entity Manager Factory bean。因此它创建了除全局用户(我放入用于测试的虚拟 bean)和事务管理器之外的所有 bean,注入(inject) AppEnvironment,然后创建全局用户和事务管理器 bean。

非常感谢任何想法!

01-Sep-2011 10:20:55  INFO Mvc Initializer starting
01-Sep-2011 10:20:55 INFO entityManagerFactory entry - App Environment is null!!!!
01-Sep-2011 10:20:55 INFO dataSource entry - App Environment is null!!!!
01-Sep-2011 10:20:55 INFO dataSource exit - App Environment is null!!!!
01-Sep-2011 10:20:55 INFO jpaAdapter entry - App Environment is null!!!!
01-Sep-2011 10:20:55 INFO jpaAdapter exit - App Environment is null!!!!
01-Sep-2011 10:20:55 INFO entityManagerFactory exit - App Environment is null!!!!
01-Sep-2011 10:20:55 INFO Fetching properties file for LOCAL environment
01-Sep-2011 10:20:55 INFO setAppEnvironment - App Environment JpaConfig = LOCAL

这是我用来连接它的示例代码的 URL:

http://blog.springsource.com/2011/02/14/spring-3-1-m1-introducing-profile/

我发现文档说明在 Spring 3.1 之前,有必要将 @AnnotationDrivenConfig 注释添加到您的可注入(inject)类中,以便 @Autowired 工作,但这在 Spring 3.1 中已被弃用 我发现了一堆帖子寻找这个注释但是注入(inject)配置对象时没有解决任何问题!

最佳答案

据我了解,@Configuration bean 是在非配置 bean 之前构造的,并且带有 @Bean 标记的方法生成的对象还没有 bean 在它们返回时;构造 bean 的顺序是未定义的(除了一些特殊的 bean 类型;标准属性文件加载器很早就被调用,因此它们发现的值可用于设置 @Configuration bean 角,扁 bean )。直接调用 bean 方法是不受欢迎的;让 Spring 在 @Bean 方法返回后连接属性时为您做这件事。

关于Spring 3.1 Java 配置 - @Autowired、@Configuration 和@Profile 挑战,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7271801/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com