我想将此设置编辑为注释方式
因为我想将这些变量保存在一个文件中(它将存储代码所需的所有动态变量)
我将使用一个java函数动态读取这些变量
但我不熟悉它
xxx.property
IP = com.mysql.jdbc.Driver
user = root
password = 1234
模型-config.xml中
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="jdbcUrl">
<value>IP</value>
</property>
<property name="user">
<value>user</value>
</property>
<property name="password">
<value>password</value>
</property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingLocations">
<list>
<value>classpath:/com/test/User.hbm.xml</value>
</list>
</property>
.....
我该怎么办?
我应该搜索哪些kyewords?
答案 0 :(得分:2)
您可以使用配置类,使用 @Configuration 注释,使用注释来处理所有配置,这是您需要的示例:
@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence-mysql.properties" })
@ComponentScan({ "package.persistence" }) //You specify the package of your entities here
public class SpringHibernateConfig {
@Autowired
private Environment env;
@Bean
public AnnotationSessionFactoryBean sessionFactory() {
AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean();
sessionFactory.setDataSource(restDataSource());
// You specify your models package to be scanned by Hibernate
sessionFactory.setPackagesToScan(new String[] { "package.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
// You configure your jdbc settings here
@Bean
public DataSource restDataSource() {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
dataSource.setUrl(env.getProperty("com.mysql.jdbc.Driver"));
dataSource.setUsername(env.getProperty("root"));
dataSource.setPassword(env.getProperty("1234"));
return dataSource;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);
return txManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
return new PersistenceExceptionTranslationPostProcessor();
}
// You specify Hibernate properties here
Properties hibernateProperties() {
return new Properties() {
{
setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
}
};
}
}
有关详细信息,请查看:
答案 1 :(得分:1)
虽然切换到Annotation是可行的方法,但如果能够简化您的工作,还有另一种方法可以不切换。您可能必须定义一个bean并将其传递给您的属性文件的名称(假设它保存在类路径中)
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>YOURPROPERTY.properties</value>
</property>
</bean>
如果你有这个,你就可以像这样改变你的bean定义
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>${IP}</value>
</property>
<property name="jdbcUrl">
<value>IP</value>
</property>
<property name="user">
<value>${user}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
我们在这里做的是,我们已经要求spring使用PropertyPlaceholderConfigurer
加载并保留属性文件,并使用此${YOURKEY}
说明符在bean定义中使用键。
答案 2 :(得分:1)
据我所知,您希望将model-config.xml
转换为基于注释的配置。为此,您需要创建@Configuration
类,您将在其中声明bean并以实用方式设置属性。这个@Configuration
类应如下所示:
@EnableWebMvc
@ComponentScan(basePackages = {"org.some.package"})
@Configuration
public class AppConfig extends WebMvcConfigurerAdapter {
@Bean(name="dataSource")
public ComboPooledDataSource getDataSource() {
// Read your properties
Properties prop = new Properties();
String propFileName = "xxx.property";
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
if (inputStream != null) {
prop.load(inputStream);
} else {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
}
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass(prop.getProperty("IP")); //?? IP = com.mysql.jdbc.Driver ??
cpds.setJdbcUrl(prop.getProperty("propertyForUrl"));
cpds.setUser(prop.getProperty("user"));
cpds.setPassword(prop.getProperty("password"));
return cpds;
}
//create the similar function for sessionFactory
}
的setter