如何为自动装配的变量提供参数值?

时间:2016-09-01 15:35:22

标签: spring spring-mvc spring-4

我想要自动变量变量:

package com.ambre.hib.dao;

public interface LangDAO {

    public String _getText();

}

package com.ambre.hib.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;

public class LangDAOImpl implements LangDAO {

    @Autowired
    private Environment env;

    private String code;

    public LangDAOImpl() {

    }

    public LangDAOImpl(String code) {
        this.code = code;
    }

    @Override
    public String _getText() {
        return env.getProperty(code);
    }

}

package com.ambre.hib.config;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBuilder;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.ambre.hib.dao.LangDAO;
import com.ambre.hib.dao.LangDAOImpl;
import com.ambre.hib.dao.UserDAO;
import com.ambre.hib.dao.UserDAOImpl;
import com.ambre.hib.model.User;

@Configuration
@ComponentScan("com.ambre.hib")
@EnableTransactionManagement
@PropertySource("classpath:lang.properties")
public class ApplicationContextConfig {

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() { // managing properties file ( languages )
        return new PropertySourcesPlaceholderConfigurer();
    }

    @Bean(name = "viewResolver")
    public InternalResourceViewResolver getViewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");
        dataSource.setUrl("jdbc:oracle:thin:@localhost:1521:xe");
        dataSource.setUsername("system");
        dataSource.setPassword("a");

        return dataSource;
    }

    @Autowired
    @Bean(name = "sessionFactory")
    public SessionFactory getSessionFactory(DataSource dataSource) {

        LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource);

        sessionBuilder.addAnnotatedClasses(User.class);

        return sessionBuilder.buildSessionFactory();
    }

    @Autowired
    @Bean(name = "transactionManager")
    public HibernateTransactionManager getTransactionManager(SessionFactory sessionFactory) {

        HibernateTransactionManager transactionManager = new HibernateTransactionManager(sessionFactory);

        return transactionManager;
    }

    @Autowired
    @Bean(name = "userDao")
    public UserDAO getUserDao(SessionFactory sessionFactory) {
        return new UserDAOImpl(sessionFactory);
    }

    @Autowired
    @Bean(name = "langDAO")
    public LangDAO getLangDAO(String code) {
        return new LangDAOImpl(code);
    }

}

package com.ambre.hib.controller;

... // imports

@Controller
public class HomeController {

    @Autowired
    private LangDAO langDAO; // how to set here the String "title.home" to the constructor ?

    @RequestMapping("/")
    public String handleRequest(Model model) throws Exception {

        model.addAttribute("titre", langDAO._getText());
        return "UserList";
    }

}

如您所见,我在控制器中自动启动了LangDAO类。但我想传递一个String作为其构造函数参数。怎么做?

2 个答案:

答案 0 :(得分:2)

由于我可以看到您已经配置了PropertySourcesPlaceholderConfigurer,因此您可以将配置文件中的属性直接自动装配到构造函数中,如下所示:

@Autowired
public LangDAOImpl(@Value("${you_property_key}") String code) {
        this.code = code;
    }

或直接在物业上

@Value("${you_property_key}") String code

或者,如果您定义了@PostConstruct方法,则可以从环境中获取该属性并手动设置它。

答案 1 :(得分:0)

值可以通过多种方式自动装配

  1. Spring提供了自动装配系统属性的功能。

    @Value( “$ {pool.size}”) public String size;

  2. 使用spring应用程序上下文传递值

    <bean id="textEditor" class="com.tutorialspoint.TextEditor">     
         <constructor-arg  value="${property.from.file}"/>
    </bean>
    
  3. 使用

    将属性加载到内存中
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <value>classpath:service.properties</value>
            </property>
        </bean>
    

    参考:http://www.tutorialspoint.com/spring/spring_autowiring_byconstructor.htm