SpringMVC4零配置--应用上下文配置【AppConfig】

从spring3.0开始,Spring将JavaConfig整合到核心模块,普通的POJO只需要标注@Configuration注解,就可以成为spring配置类,并通过在方法上标注@Bean注解的方式注入bean。

 

Xml配置和Java类配置对比如下:

applicationContext-AppConfig.xml

 


	
	
	
        
    
    
    
    
    

 AppConfig.java

@Configuration
@ComponentScan(basePackages = "web.function", excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION, value = { Controller.class }) })
@EnableAspectJAutoProxy(proxyTargetClass=true)
@Import({CachingConfig.class,DaoConfig.class})
public class AppConfig {
}

 

 

applicationContext-CachingConfig.xml

 

 

	
	

	
	

	
	

CachingConfig.java 

@Configuration
@EnableCaching// 
public class CachingConfig {
	private static final Logger logger = Logger.getLogger(CachingConfig.class);


	@Bean
	public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
		EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
		ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource(
				"config/ehcache.xml"));
		return ehCacheManagerFactoryBean;
	}

	@Bean
	public CacheManager cacheManager() {
		logger.info("EhCacheCacheManager");
		EhCacheCacheManager cacheManager = new EhCacheCacheManager();
		cacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject());
		return cacheManager;
	}
}

applicationContext-DaoConfig.xml

 

 



	
	
		
		
			
				web.function.**.model.oracle
			
		
		
			
				
					${hibernate.dialect}
				
				${hibernate.show_sql}
				org.springframework.orm.hibernate4.SpringSessionContext
			
		
	

	
	
	
	  
	
	
	
	
	
	
		
	
	
	
	

DaoConfig.java

@Configuration
//启用注解事务管理,使用CGLib代理
@EnableTransactionManagement(proxyTargetClass = true)
@Import({DataSourceConfig.class})
public class DaoConfig {

	private static final Logger logger = Logger.getLogger(DaoConfig.class);

	
	@Value("${hibernate.dialect}")
	String hibernate_dialect;
	@Value("${hibernate.show_sql}")
	String hibernate_show_sql;
	
	/**                                                          
	* 描述 : <负责解析资源文件>. 
*

<这个类必须有,而且必须声明为static,否则不能正常解析>

* @return */ @Bean public static PropertySourcesPlaceholderConfigurer placehodlerConfigurer() { logger.info("PropertySourcesPlaceholderConfigurer"); return new PropertySourcesPlaceholderConfigurer(); } @Resource(name="dataSource") public DataSource dataSource; @Bean(name = "sessionFactory") public LocalSessionFactoryBean localSessionFactoryBean() { logger.info("sessionFactory"); LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean(); sessionFactory.setDataSource(dataSource); String[] packagesToScan = new String[] { "web.function.**.model.oracle" }; sessionFactory.setPackagesToScan(packagesToScan); Properties hibernateProperties = new Properties(); hibernateProperties.setProperty("hibernate.dialect", hibernate_dialect); hibernateProperties.setProperty("hibernate.show_sql", hibernate_show_sql); hibernateProperties.setProperty( "hibernate.current_session_context_class", "org.springframework.orm.hibernate4.SpringSessionContext"); sessionFactory.setHibernateProperties(hibernateProperties); return sessionFactory; } @Bean(name = "hibernateDAO") public CP_Hibernate4DAOImpl hibernate4Dao() { logger.info("hibernateDAO"); CP_Hibernate4DAOImpl dao = new CP_Hibernate4DAOImpl(); dao.setSessionFactory(localSessionFactoryBean().getObject()); return dao; } @Bean(name = "transactionManager") public HibernateTransactionManager hibernateTransactionManager() { logger.info("transactionManager"); HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager(); hibernateTransactionManager.setSessionFactory(localSessionFactoryBean() .getObject()); return hibernateTransactionManager; } }

 

 

applicationContext-DataSourceConfig.xml



	
	
		
		
		
		
	

 

DataSourceConfig.java

@Configuration
//加载资源文件
@PropertySource({"classpath:/config/properties/db.properties"})
public class DataSourceConfig {
	private static final Logger logger = Logger.getLogger(DataSourceConfig.class);
	/*
	 * 绑定资源属性
	 */
	@Value("${jdbc.driver}")
	String driverClass;
	@Value("${jdbc.url}")
	String url;
	@Value("${jdbc.username}")
	String userName;
	@Value("${jdbc.password}")
	String passWord;
	
	@Bean(name = "dataSource")
	public DataSource dataSource() {
		logger.info("DataSource");
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setDriverClassName(driverClass);
		dataSource.setUrl(url);
		dataSource.setUsername(userName);
		dataSource.setPassword(passWord);
		return dataSource;
	}
}

 

DataSource如果使用JNDI的方式如下:

xml配置:

Java类配置:

@Bean
	public JndiObjectFactoryBean jndiObjectFactoryBean(){
		JndiObjectFactoryBean factory = new JndiObjectFactoryBean();
	//	factory.setJndiName("java:comp/env/jdbc/demoDB"); //两种方式均可,spring会自动补齐
                factory.setJndiName("jdbc/demoDB");
		return factory;
	}
	
	@Bean(name = "dataSource")
	public DataSource dataSource() throws Exception{
		logger.info("DataSourceJNDI");
		return (DataSource)jndiObjectFactoryBean().getObject();
		
	}

 

SpringMVC4零配置 :代码下载

SpringMVC4零配置--web.xml

SpringMVC4零配置--应用上下文配置【AppConfig】

SpringMVC4零配置--SpringSecurity相关配置【SpringSecurityConfig】

SpringMVC4零配置--Web上下文配置【MvcConfig】

 

 

你可能感兴趣的:(Spring)