在spring 4应用程序中,我创建了两个bean,如下所示:
<!-- schemaFactory-->
<bean id="schemaFact" class="javax.xml.validation.SchemaFactory"
factory-method="newInstance">
<constructor-arg value="http://www.w3.org/2001/XMLSchema" />
</bean>
<!-- schema -->
<bean id="schema" class="javax.xml.validation.Schema"
factory-bean="schemaFact" factory-method="newSchema">
<constructor-arg value="classpath:/configs/sample.xsd" />
</bean>
一个生成模式工厂,另一个使用工厂(根据xsd验证xml)。
在代码中:
public class example {
@Inject
private Schema aschema;
......
private validate(){
Validator validator = aschema.newValidator();
validator.validate(xmlFile);
}
}
这很好用,但是我想知道是否可以用一种我们不需要单独定义工厂bean的方式来编写这个spring.xml
(并且最后有一个更简单的spring.xml
) 。正如您所看到的,我只需要在我的schema
课程中注入example
bean,而schemaFact
根本不需要{<1}}。
答案 0 :(得分:0)
无论是否直接使用它们,注入良好的依赖关系都必须使用spring bean。
因此,即使您没有在代码中直接使用SchemaFactory
,而是将其作为依赖项注入另一个spring bean,两者都必须是spring managed beans
您可以考虑使用注释,然后根本不需要弹簧文件。
答案 1 :(得分:0)
这是在java类中创建bean的一些示例代码。可以在这里创建任何bean并在相关类中自动连线。
这个注释为我们提供了很大的支持ex- @Configuration
import java.util.Properties;
import javax.persistence.SharedCacheMode;
import........
@Configuration
@ComponentScan(basePackages = {"<package>"})
@EnableTransactionManagement
@PropertySources(value = {@PropertySource(value = {"<property file path>"})})
public class ModulesConfig {
private static final Logger log = LoggerFactory.getLogger(ModulesConfig.class);
@Autowired
private Environment environment;
@Bean(destroyMethod = "close")
public BoneCPDataSource getDataSource() {
BoneCPDataSource dataSource = new BoneCPDataSource();
dataSource.setDriverClass(environment.getProperty("database.driver"));
dataSource.setJdbcUrl(environment.getProperty("database.url"));
dataSource.setUsername(environment.getProperty("database.username"));
dataSource.setPassword(environment.getProperty("database.password"));
dataSource.setIdleConnectionTestPeriodInMinutes(30);
dataSource.setMaxConnectionsPerPartition(5);
dataSource.setMinConnectionsPerPartition(2);
dataSource.setPartitionCount(3);
dataSource.setAcquireIncrement(2);
dataSource.setStatementsCacheSize(100);
return dataSource;
}
@Bean
@Primary
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
Properties jpaProperties = new Properties();
jpaProperties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
jpaProperties.setProperty("hibernate.showSql", "false");
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setDataSource(getDataSource());
em.setPersistenceUnitName("entityManagerFactory");
em.setPackagesToScan("<packages>");
em.setJpaVendorAdapter(vendorAdapter);
em.setJpaProperties(jpaProperties);
em.setSharedCacheMode(SharedCacheMode.ENABLE_SELECTIVE);
return em;
}
@Bean(name = "transactionManager1")
@Primary
public JpaTransactionManager getTransactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());
return transactionManager;
}
@Bean(name = "transactionManager2")
public JpaTransactionManager getTransactionManager2() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory2().getObject());
return transactionManager;
}
@Bean
@Qualifier("VoucherServiceMarshaller")
public Jaxb2Marshaller getVoucherServiceMarshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath(environment.getProperty("voucher.service.marshaller.contextPath"));
return marshaller;
}
@Bean
@Qualifier("VoucherServiceTemplate")
public WebServiceTemplate getVoucherServiceTemplate() {
WebServiceTemplate template = new WebServiceTemplate(getVoucherServiceMarshaller());
template.setDefaultUri(environment.getProperty("voucher.service.defaultUri"));
return template;
}
@Bean
public VoucherServiceProxy getVoucherServiceProxy() {
VoucherServiceProxy voucherServiceProxy = new VoucherServiceProxy();
return voucherServiceProxy;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
@Bean
public JavaMailSender javaMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
Properties mailProperties = new Properties();
mailProperties.put("mail.smtp.host", environment.getProperty("mail.smtp.host"));
mailProperties.put("mail.smtp.socketFactory.port", environment.getProperty("mail.smtp.socketFactory.port"));
mailProperties.put("mail.smtp.socketFactory.class", environment.getProperty("mail.smtp.socketFactory.class"));
mailProperties.put("mail.smtp.auth", environment.getProperty("mail.smtp.auth"));
mailSender.setJavaMailProperties(mailProperties);
mailSender.setUsername(environment.getProperty("mail.username"));
mailSender.setPassword(environment.getProperty("mail.password"));
return mailSender;
}
@Bean
public ChargingGatewayServiceProxy getChargingGatewayServiceProxy() {
ChargingGatewayServiceProxy chargingGatewayServiceProxy = new ChargingGatewayServiceProxy();
return chargingGatewayServiceProxy;
}
@Bean
@Qualifier("ChargingGatewayServiceMarshaller")
public Jaxb2Marshaller getChargingGatewayServiceMarshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath(environment.getProperty("cg.service.marshaller.contextPath1"));
return marshaller;
}
@Bean
@Qualifier("ChargingGatewayServiceTemplate")
public WebServiceTemplate getChargingGatewayServiceTemplate() {
WebServiceTemplate template = new WebServiceTemplate(getChargingGatewayServiceMarshaller());
template.setDefaultUri(environment.getProperty("cg.service.url"));
template.setMessageSender(getMessageSender());
return template;
}
@Bean
public HttpComponentsMessageSender getMessageSender() {
HttpComponentsMessageSender httpComponentsMessageSender = new HttpComponentsMessageSender();
httpComponentsMessageSender.setConnectionTimeout(Integer.parseInt(environment.getProperty("cg.connection.timeout")));
httpComponentsMessageSender.setReadTimeout(Integer.parseInt(environment.getProperty("cg.read.timeout")));
return httpComponentsMessageSender;
}
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(clientHttpRequestFactory());
}
private ClientHttpRequestFactory clientHttpRequestFactory() {
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setReadTimeout(50000);
factory.setConnectTimeout(50000);
return factory;
}
@Bean
public Cache cacheTemp() {
net.sf.ehcache.CacheManager cm = net.sf.ehcache.CacheManager.create().getInstance();
cm.addCache("cacheTemp");
Cache cache = cm.getCache("cacheTemp");
return cache;
}
@Bean
public Cache cacheActive() {
net.sf.ehcache.CacheManager cm = net.sf.ehcache.CacheManager.create().getInstance();
cm.addCache("cacheActive");
Cache cache = cm.getCache("cacheActive");
return cache;
}
@Bean
public Cache cache() {
net.sf.ehcache.CacheManager cm = net.sf.ehcache.CacheManager.create().getInstance();
cm.addCache("cache");
Cache cache = cm.getCache("cache");
return cache;
}
}