配置类如下(直接复制的以前项目的配置):
@Configuration //标注此类为配置类(必有)
@ComponentScan(basePackageClasses = AppConfig.class, scopeResolver = DefaultAnnotationScopeResolver.class) //Spring Mvc的扫描地址配置(非必需)
@EnableTransactionManagement //加入事务管理(非必需)
@EnableAspectJAutoProxy //加入AspectJ的动态代理(非必需)
@PropertySource({"classpath:site-jdbc.properties", "classpath:site-social.properties", "classpath:site-device.properties"})
//加载properties文件,后面就可以直接使用
public class AppConfig {
@Bean //标注返回对象为一个Bean,方法内为Bean的具体初始化方式
@Qualifier("messageSource")//标注Bean的name默认为方法名
public ResourceBundleMessageSource messageSource() {
ResourceBundleMessageSource bundleMessageSource = new ResourceBundleMessageSource();
bundleMessageSource.setBasename("i18n.nstechs-i18n");
bundleMessageSource.setUseCodeAsDefaultMessage(true);
return bundleMessageSource;
}
@Bean
public SessionLocaleResolver localeResolver() {
SessionLocaleResolver localeResolver = new SessionLocaleResolver();
return localeResolver;
}
@Bean
public TilesViewResolver viewResolver() {
return new TilesViewResolver();
}
@Bean
public TilesConfigurer tilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
tilesConfigurer.setDefinitions(new String[] { "classpath*:config/tiles/website-tiles.xml", "classpath*:config/tiles/common-tiles.xml" });
return tilesConfigurer;
}
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
return multipartResolver;
}
@Bean
public SQSAccess sqsAccess() {
SQSAccess sqsAccess = new SQSAccess();
sqsAccess.setSqsUrl(env.getProperty("site.sqs.address"));
return sqsAccess;
}
@Bean
public DynamoAccess dynamoAccess() {
DynamoAccess dynamoAccess = new DynamoAccess();
return dynamoAccess;
}
@Bean
public ImageClient imageClient() {
ImageClient imageClient = new ImageClient();
imageClient.setReadJdbcAccess(jdbcAccessFactory().getSlaveJDBCAccess());
return imageClient;
}
@Bean
public ElasticSearchAccess elasticSearchAccess() {
return new ElasticSearchAccess();
}
@Bean
public I18nUtil i18nUtil() {
return new I18nUtil();
}
@Bean
public MailSender mailSender() {
MailSender mailSender = new MailSender();
mailSender.setHost(env.getProperty("site.mail.host"));
mailSender.setPort(env.getProperty("site.mail.port", int.class));
mailSender.setUsername(env.getProperty("site.mail.username"));
mailSender.setPassword(env.getProperty("site.mail.password"));
return mailSender;
}
@Bean
public VelocityEngineFactoryBean velocityEngine() {
VelocityEngineFactoryBean velocityEngine = new VelocityEngineFactoryBean();
velocityEngine.setResourceLoaderPath(env.getProperty("site.mail.template.location"));
velocityEngine.setPreferFileSystemAccess(false);
Map<String, Object> map = new HashMap<String, Object>();
map.put("default.contentType", "text/html; charset=utf-8");
map.put("output.encoding", "utf-8");
map.put("input.encoding", "utf-8");
velocityEngine.setVelocityPropertiesMap(map);
return velocityEngine;
}
@Bean
public DBSwitchAdvisor dbSwitchAdvisor() {
return new DBSwitchAdvisor(DBSwitchInterceptor.class);
}
@Bean
@Scope(value = “singleton”, proxyMode = ScopedProxyMode.TARGET_CLASS)
//Bean的scope默认为singleton,后面的proxyMode是代理模式这里使用AspectJ的类代理(后面Aop是讲解)
public JDBCAccessContext jdbcAccessContext() {
return new JDBCAccessContext();
}
}
在类中需要注入的属性上使用@Autowired表示,Spring会自动注入。
若在J2SE中使用spring,请使用
ApplicationConText ctx = new AnnotationConfigApplicationContext(配置类);初始化Spring容器
在Web应用中,需要在Web.Xml中如下配置(直接用的SpringMvc配置):
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.nstechs.commerce.AppConfig</param-value>
</init-param>
<!-- use annotation replace xml configuration. @Configuration class is required. -->
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
在Servlet3.0出来后甚至连Web.xml都可以直接用注解代替,有兴趣的参考
http://my.oschina.net/u/1413049/blog/190175