spring學習筆記(三)

基於Java類的Spring配置

配置類以下(直接複製的之前項目的配置): html

 

@Configuration  //標註此類爲配置類(必有) java

@ComponentScan(basePackageClasses = AppConfig.class, scopeResolver = DefaultAnnotationScopeResolver.class) //Spring Mvc的掃描地址配置(非必需) web

@EnableTransactionManagement //加入事務管理(非必需) spring

@EnableAspectJAutoProxy     //加入AspectJ的動態代理(非必需) app

@PropertySource({"classpath:site-jdbc.properties", "classpath:site-social.properties", "classpath:site-device.properties"}) ui

//加載properties文件,後面就能夠直接使用 url

public class AppConfig { spa

 

    @Bean //標註返回對象爲一個Bean,方法內爲Bean的具體初始化方式 .net

    @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

相關文章
相關標籤/搜索