Spring 4+Redis+ActiveMQ+MongoDB+MySql+Quartz 整合 註解方式
一:目錄結構
二:統一配置檔案
context-datasource.properties
systemConfig.properties
三:配置檔案
四:Demo
二:統一配置檔案
context-datasource.properties
點選(此處)摺疊或開啟
-
workDesk.jdbc.driverclass=com.mysql.jdbc.Driver
-
workDesk.jdbc.url=jdbc:mysql://10.243.3.18:3306/system?userUnicode=true&characterEncoding=UTF-8
-
workDesk.jdbc.username=root
-
workDesk.jdbc.password=$Fortune2015
-
workDesk.jdbc.poolsize.max=3
-
workDesk.jdbc.poolsize.min=3
-
workDesk.jdbc.poolsize.initial=2
-
workDesk.jdbc.idletime.max=25000
-
workDesk.jdbc.idleConnectionTestPeriod=18000
-
-
#-------workDesk jdbc--------
-
workDesk.read.jdbc.driverclass=com.mysql.jdbc.Driver
-
workDesk.read.jdbc.url=jdbc:mysql://10.243.3.18:3306/system?userUnicode=true&characterEncoding=UTF-8
-
workDesk.read.jdbc.username=root
-
workDesk.read.jdbc.password=$Fortune2015
-
workDesk.read.jdbc.poolsize.max=3
-
workDesk.read.jdbc.poolsize.min=3
-
workDesk.read.jdbc.poolsize.initial=2
-
workDesk.read.jdbc.idletime.max=25000
-
workDesk.read.jdbc.idleConnectionTestPeriod=18000
-
-
-
#--------------redis conf------------
-
redis.ip=10.243.3.18
-
redis.port=6379
-
redis.password=$Fortune2015
-
redis.default.db=0
-
redis.timeout=30000
-
-
redis.pool.maxActive=1024
-
redis.pool.maxIdle=200
-
redis.pool.maxWait=1000
-
redis.pool.testOnBorrow=true
-
-
-
#------------------jms server-------------
-
activeMq=failover:(tcp://10.243.3.18:61616,tcp://10.243.3.18:61616)?randomize=false
-
-
#--------------mongodb conf------------
-
mongodb.host=10.243.3.27
-
mongodb.port=27017
-
mongodb.database=gmap
-
mongodb.username=mongodb
- mongodb.password=$Fortune2016
點選(此處)摺疊或開啟
-
#=======================================================================================
-
-
#MailSender Config
-
#=======================================================================================
- mail.host=smtp.mxhichina.com
點選(此處)摺疊或開啟
-
@Configuration
-
@EnableScheduling
-
@EnableCaching
-
@EnableTransactionManagement(proxyTargetClass = true)
-
@ComponentScan("com.gemdale,com.enjoylink")
-
@Import({DataSourceElConfig.class,TransactionManagerElConfig.class,CachingElConfig.class,MongoElConfig.class,JmsElConfig.class})
-
public class AppConfig {
-
- }
點選(此處)摺疊或開啟
-
@Configuration
-
public class CachingElConfig {
-
-
@Autowired
-
@Qualifier("jedisConnectionFactory")
-
private JedisConnectionFactory jedisConnectionFactory;
-
-
@Autowired
-
@Qualifier("keySerializer")
-
private StringRedisSerializer keySerializer;
-
-
@Autowired
-
@Qualifier("valueSerializer")
-
private JdkSerializationRedisSerializer valueSerializer;
-
-
@Bean(name = "redisTemplate")
-
public RedisTemplate<String, Object> redisTemplate() {
-
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
-
redisTemplate.setConnectionFactory(jedisConnectionFactory);
-
redisTemplate.setKeySerializer(keySerializer);
-
redisTemplate.setHashKeySerializer(keySerializer);
-
redisTemplate.setValueSerializer(valueSerializer);
-
redisTemplate.setHashValueSerializer(valueSerializer);
-
redisTemplate.afterPropertiesSet();
-
return redisTemplate;
-
}
-
-
@Bean
-
public CacheManager cacheManager(RedisTemplate<String, Object> redisTemplate) {
-
RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate);
-
// 設定過期時間
-
// cacheManager.setDefaultExpiration(10);
-
return cacheManager;
-
-
}
- }
點選(此處)摺疊或開啟
-
@Configuration
-
@PropertySource("classpath:context-datasource.properties")
-
public class DataSourceElConfig {
-
-
private static Logger logger = Logger.getLogger(DataSourceElConfig.class);
-
-
@Value("${workDesk.jdbc.driverclass}")
-
private String driverClass;
-
@Value("${workDesk.jdbc.url}")
-
private String jdbcUrl;
-
@Value("${workDesk.jdbc.username}")
-
private String user;
-
@Value("${workDesk.jdbc.password}")
-
private String password;
-
@Value("${workDesk.jdbc.poolsize.max}")
-
private int maxPoolSize;
-
@Value("${workDesk.jdbc.poolsize.min}")
-
private int minPoolSize;
-
@Value("${workDesk.jdbc.poolsize.initial}")
-
private int initialPoolSize;
-
@Value("${workDesk.jdbc.idletime.max}")
-
private int maxIdleTime;
-
@Value("${workDesk.jdbc.idleConnectionTestPeriod}")
-
private int idleConnectionTestPeriod;
-
-
@Value("${workDesk.read.jdbc.driverclass}")
-
private String readDriverClass;
-
@Value("${workDesk.read.jdbc.url}")
-
private String readJdbcUrl;
-
@Value("${workDesk.read.jdbc.username}")
-
private String readUser;
-
@Value("${workDesk.read.jdbc.password}")
-
private String readPassword;
-
@Value("${workDesk.read.jdbc.poolsize.max}")
-
private int readMaxPoolSize;
-
@Value("${workDesk.read.jdbc.poolsize.min}")
-
private int readMinPoolSize;
-
@Value("${workDesk.read.jdbc.poolsize.initial}")
-
private int readInitialPoolSize;
-
@Value("${workDesk.read.jdbc.idletime.max}")
-
private int readMaxIdleTime;
-
@Value("${workDesk.read.jdbc.idleConnectionTestPeriod}")
-
private int readIdleConnectionTestPeriod;
-
-
@Value("${redis.pool.maxActive}")
-
private int maxTotal;
-
@Value("${redis.pool.maxIdle}")
-
private int maxIdle;
-
@Value("${redis.pool.maxWait}")
-
private long maxWaitMillis;
-
-
@Value("${redis.ip}")
-
private String redisIp;
-
@Value("${redis.port}")
-
private int redisPort;
-
@Value("${redis.password}")
-
private String redisPassword;
-
@Value("${redis.timeout}")
-
private int redisTimeout;
-
@Value("${redis.default.db}")
-
private int redisDb;
-
-
@Value("${mongodb.host}")
-
private String mongoHost;
-
@Value("${mongodb.port}")
-
private int mongoPort;
-
@Value("${mongodb.database}")
-
private String mongoUserName;
-
@Value("${mongodb.username}")
-
private String mongoDB;
-
@Value("${mongodb.password}")
-
private String mongoPassword;
-
-
@Value("${activeMq}")
-
private String activeMq;
-
-
@Autowired
-
private Environment environment;
-
-
@Bean
-
public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
-
return new PropertySourcesPlaceholderConfigurer();
-
}
-
-
/**
-
* MySql Master 寫庫資料來源
-
*
-
* @return
-
*/
-
@Bean(name = "platformTomcat", destroyMethod = "close")
-
public ComboPooledDataSource platformTomcat() {
-
ComboPooledDataSource dataSource = new ComboPooledDataSource();
-
try {
-
dataSource.setDriverClass(driverClass);
-
}
-
catch (PropertyVetoException e) {
-
logger.error("資料來源配置,無法找到配置類", e);
-
}
-
dataSource.setJdbcUrl(jdbcUrl);
-
dataSource.setUser(user);
-
dataSource.setPassword(password);
-
dataSource.setMaxPoolSize(maxPoolSize);
-
dataSource.setMinPoolSize(minPoolSize);
-
dataSource.setInitialPoolSize(initialPoolSize);
-
dataSource.setMaxIdleTime(maxIdleTime);
-
dataSource.setTestConnectionOnCheckin(true);
-
dataSource.setPreferredTestQuery("SELECT 1");
-
dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
-
return dataSource;
-
}
-
-
/**
-
* MySql Slave 讀庫資料來源
-
*
-
* @return
-
*/
-
@Bean(name = "platformReadTomcat", destroyMethod = "close")
-
public ComboPooledDataSource platformReadTomcat() {
-
ComboPooledDataSource dataSource = new ComboPooledDataSource();
-
try {
-
dataSource.setDriverClass(readDriverClass);
-
}
-
catch (PropertyVetoException e) {
-
logger.error("資料來源配置,無法找到配置類", e);
-
}
-
dataSource.setJdbcUrl(readJdbcUrl);
-
dataSource.setUser(readUser);
-
dataSource.setPassword(readPassword);
-
dataSource.setMaxPoolSize(readMaxPoolSize);
-
dataSource.setMinPoolSize(readMinPoolSize);
-
dataSource.setInitialPoolSize(readInitialPoolSize);
-
dataSource.setMaxIdleTime(readMaxIdleTime);
-
dataSource.setTestConnectionOnCheckin(true);
-
dataSource.setPreferredTestQuery("SELECT 1");
-
dataSource.setIdleConnectionTestPeriod(readIdleConnectionTestPeriod);
-
return dataSource;
-
}
-
-
/**
-
* Redis 連線池配置資訊
-
*
-
* @return
-
*/
-
@Bean(name = "jedisPoolConfig")
-
public JedisPoolConfig jedisPoolConfig() {
-
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
-
jedisPoolConfig.setMaxTotal(maxTotal);
-
jedisPoolConfig.setMaxIdle(maxIdle);
-
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
-
jedisPoolConfig.setTestOnBorrow(true);
-
return jedisPoolConfig;
-
}
-
-
@Bean(name = "jedisConnectionFactory")
-
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
-
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
-
jedisConnectionFactory.setUsePool(true);
-
jedisConnectionFactory.setHostName(redisIp);
-
jedisConnectionFactory.setPort(redisPort);
-
jedisConnectionFactory.setPassword(redisPassword);
-
jedisConnectionFactory.setTimeout(redisTimeout);
-
jedisConnectionFactory.setDatabase(redisDb);
-
jedisConnectionFactory.afterPropertiesSet();
-
return jedisConnectionFactory;
-
}
-
-
@Bean(name = "keySerializer")
-
public StringRedisSerializer keySerializer() {
-
return new StringRedisSerializer();
-
}
-
-
@Bean(name = "valueSerializer")
-
public JdkSerializationRedisSerializer valueSerializer() {
-
return new JdkSerializationRedisSerializer();
-
}
-
-
/**
-
* MongoDB 配置
-
*
-
* @return
-
*/
-
@Bean(name = "mongoDbFactory")
-
public MongoDbFactory mongoDbFactory() throws Exception {
-
-
MongoCredential credential = MongoCredential.createCredential(mongoUserName, mongoDB,
-
mongoPassword.toCharArray());
-
ServerAddress serverAddress = new ServerAddress(mongoHost, mongoPort);
-
MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(credential));
-
SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, mongoDB);
-
return mongoDbFactory;
-
}
-
-
@Bean(name="mongoConverter")
-
public MongoConverter mongoConverter(MongoDbFactory mongoDbFactory) {
-
-
MappingMongoConverter mongoConverter = new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory),
-
new MongoMappingContext());
-
// 不插入_class
-
mongoConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
-
return mongoConverter;
-
}
-
-
-
/**
-
* Jms 配置
-
* @return
-
*/
-
@Bean(name="activeMQConnectionFactory")
-
public ActiveMQConnectionFactory activeMQConnectionFactory()
-
{
-
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
-
activeMQConnectionFactory.setUseAsyncSend(false);
-
activeMQConnectionFactory.setBrokerURL(activeMq);
-
return activeMQConnectionFactory;
-
}
-
-
@Bean(name="cachingConnectionFactory")
-
public CachingConnectionFactory cachingConnectionFactory(ActiveMQConnectionFactory targetConnectionFactory)
-
{
-
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(targetConnectionFactory);
-
cachingConnectionFactory.setSessionCacheSize(10);
-
return cachingConnectionFactory;
-
}
- }
點選(此處)摺疊或開啟
-
@Configuration
-
@PropertySource("classpath:context-datasource.properties")
-
public class DataSourceElConfig {
-
-
private static Logger logger = Logger.getLogger(DataSourceElConfig.class);
-
-
@Value("${workDesk.jdbc.driverclass}")
-
private String driverClass;
-
@Value("${workDesk.jdbc.url}")
-
private String jdbcUrl;
-
@Value("${workDesk.jdbc.username}")
-
private String user;
-
@Value("${workDesk.jdbc.password}")
-
private String password;
-
@Value("${workDesk.jdbc.poolsize.max}")
-
private int maxPoolSize;
-
@Value("${workDesk.jdbc.poolsize.min}")
-
private int minPoolSize;
-
@Value("${workDesk.jdbc.poolsize.initial}")
-
private int initialPoolSize;
-
@Value("${workDesk.jdbc.idletime.max}")
-
private int maxIdleTime;
-
@Value("${workDesk.jdbc.idleConnectionTestPeriod}")
-
private int idleConnectionTestPeriod;
-
-
@Value("${workDesk.read.jdbc.driverclass}")
-
private String readDriverClass;
-
@Value("${workDesk.read.jdbc.url}")
-
private String readJdbcUrl;
-
@Value("${workDesk.read.jdbc.username}")
-
private String readUser;
-
@Value("${workDesk.read.jdbc.password}")
-
private String readPassword;
-
@Value("${workDesk.read.jdbc.poolsize.max}")
-
private int readMaxPoolSize;
-
@Value("${workDesk.read.jdbc.poolsize.min}")
-
private int readMinPoolSize;
-
@Value("${workDesk.read.jdbc.poolsize.initial}")
-
private int readInitialPoolSize;
-
@Value("${workDesk.read.jdbc.idletime.max}")
-
private int readMaxIdleTime;
-
@Value("${workDesk.read.jdbc.idleConnectionTestPeriod}")
-
private int readIdleConnectionTestPeriod;
-
-
@Value("${redis.pool.maxActive}")
-
private int maxTotal;
-
@Value("${redis.pool.maxIdle}")
-
private int maxIdle;
-
@Value("${redis.pool.maxWait}")
-
private long maxWaitMillis;
-
-
@Value("${redis.ip}")
-
private String redisIp;
-
@Value("${redis.port}")
-
private int redisPort;
-
@Value("${redis.password}")
-
private String redisPassword;
-
@Value("${redis.timeout}")
-
private int redisTimeout;
-
@Value("${redis.default.db}")
-
private int redisDb;
-
-
@Value("${mongodb.host}")
-
private String mongoHost;
-
@Value("${mongodb.port}")
-
private int mongoPort;
-
@Value("${mongodb.database}")
-
private String mongoUserName;
-
@Value("${mongodb.username}")
-
private String mongoDB;
-
@Value("${mongodb.password}")
-
private String mongoPassword;
-
-
@Value("${activeMq}")
-
private String activeMq;
-
-
@Autowired
-
private Environment environment;
-
-
@Bean
-
public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
-
return new PropertySourcesPlaceholderConfigurer();
-
}
-
-
/**
-
* MySql Master 寫庫資料來源
-
*
-
* @return
-
*/
-
@Bean(name = "platformTomcat", destroyMethod = "close")
-
public ComboPooledDataSource platformTomcat() {
-
ComboPooledDataSource dataSource = new ComboPooledDataSource();
-
try {
-
dataSource.setDriverClass(driverClass);
-
}
-
catch (PropertyVetoException e) {
-
logger.error("資料來源配置,無法找到配置類", e);
-
}
-
dataSource.setJdbcUrl(jdbcUrl);
-
dataSource.setUser(user);
-
dataSource.setPassword(password);
-
dataSource.setMaxPoolSize(maxPoolSize);
-
dataSource.setMinPoolSize(minPoolSize);
-
dataSource.setInitialPoolSize(initialPoolSize);
-
dataSource.setMaxIdleTime(maxIdleTime);
-
dataSource.setTestConnectionOnCheckin(true);
-
dataSource.setPreferredTestQuery("SELECT 1");
-
dataSource.setIdleConnectionTestPeriod(idleConnectionTestPeriod);
-
return dataSource;
-
}
-
-
/**
-
* MySql Slave 讀庫資料來源
-
*
-
* @return
-
*/
-
@Bean(name = "platformReadTomcat", destroyMethod = "close")
-
public ComboPooledDataSource platformReadTomcat() {
-
ComboPooledDataSource dataSource = new ComboPooledDataSource();
-
try {
-
dataSource.setDriverClass(readDriverClass);
-
}
-
catch (PropertyVetoException e) {
-
logger.error("資料來源配置,無法找到配置類", e);
-
}
-
dataSource.setJdbcUrl(readJdbcUrl);
-
dataSource.setUser(readUser);
-
dataSource.setPassword(readPassword);
-
dataSource.setMaxPoolSize(readMaxPoolSize);
-
dataSource.setMinPoolSize(readMinPoolSize);
-
dataSource.setInitialPoolSize(readInitialPoolSize);
-
dataSource.setMaxIdleTime(readMaxIdleTime);
-
dataSource.setTestConnectionOnCheckin(true);
-
dataSource.setPreferredTestQuery("SELECT 1");
-
dataSource.setIdleConnectionTestPeriod(readIdleConnectionTestPeriod);
-
return dataSource;
-
}
-
-
/**
-
* Redis 連線池配置資訊
-
*
-
* @return
-
*/
-
@Bean(name = "jedisPoolConfig")
-
public JedisPoolConfig jedisPoolConfig() {
-
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
-
jedisPoolConfig.setMaxTotal(maxTotal);
-
jedisPoolConfig.setMaxIdle(maxIdle);
-
jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);
-
jedisPoolConfig.setTestOnBorrow(true);
-
return jedisPoolConfig;
-
}
-
-
@Bean(name = "jedisConnectionFactory")
-
public JedisConnectionFactory jedisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
-
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);
-
jedisConnectionFactory.setUsePool(true);
-
jedisConnectionFactory.setHostName(redisIp);
-
jedisConnectionFactory.setPort(redisPort);
-
jedisConnectionFactory.setPassword(redisPassword);
-
jedisConnectionFactory.setTimeout(redisTimeout);
-
jedisConnectionFactory.setDatabase(redisDb);
-
jedisConnectionFactory.afterPropertiesSet();
-
return jedisConnectionFactory;
-
}
-
-
@Bean(name = "keySerializer")
-
public StringRedisSerializer keySerializer() {
-
return new StringRedisSerializer();
-
}
-
-
@Bean(name = "valueSerializer")
-
public JdkSerializationRedisSerializer valueSerializer() {
-
return new JdkSerializationRedisSerializer();
-
}
-
-
/**
-
* MongoDB 配置
-
*
-
* @return
-
*/
-
@Bean(name = "mongoDbFactory")
-
public MongoDbFactory mongoDbFactory() throws Exception {
-
-
MongoCredential credential = MongoCredential.createCredential(mongoUserName, mongoDB,
-
mongoPassword.toCharArray());
-
ServerAddress serverAddress = new ServerAddress(mongoHost, mongoPort);
-
MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(credential));
-
SimpleMongoDbFactory mongoDbFactory = new SimpleMongoDbFactory(mongoClient, mongoDB);
-
return mongoDbFactory;
-
}
-
-
@Bean(name="mongoConverter")
-
public MongoConverter mongoConverter(MongoDbFactory mongoDbFactory) {
-
-
MappingMongoConverter mongoConverter = new MappingMongoConverter(new DefaultDbRefResolver(mongoDbFactory),
-
new MongoMappingContext());
-
// 不插入_class
-
mongoConverter.setTypeMapper(new DefaultMongoTypeMapper(null));
-
return mongoConverter;
-
}
-
-
-
/**
-
* Jms 配置
-
* @return
-
*/
-
@Bean(name="activeMQConnectionFactory")
-
public ActiveMQConnectionFactory activeMQConnectionFactory()
-
{
-
ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory();
-
activeMQConnectionFactory.setUseAsyncSend(false);
-
activeMQConnectionFactory.setBrokerURL(activeMq);
-
return activeMQConnectionFactory;
-
}
-
-
@Bean(name="cachingConnectionFactory")
-
public CachingConnectionFactory cachingConnectionFactory(ActiveMQConnectionFactory targetConnectionFactory)
-
{
-
CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(targetConnectionFactory);
-
cachingConnectionFactory.setSessionCacheSize(10);
-
return cachingConnectionFactory;
-
}
- }
點選(此處)摺疊或開啟
-
@Configuration
-
public class JmsElConfig {
-
-
@Autowired
-
@Qualifier("cachingConnectionFactory")
-
private CachingConnectionFactory cachingConnectionFactory;
-
-
@Bean(name = "jmsTemplate")
-
public JmsTemplate jmsTemplate() {
-
JmsTemplate jmsTemplate = new JmsTemplate();
-
jmsTemplate.setConnectionFactory(cachingConnectionFactory);
-
return jmsTemplate;
-
}
-
-
@Bean(name = "serviceDemoDestination")
-
public Destination serviceDemoDestination() {
-
ActiveMQQueue serviceDemoQueue = new ActiveMQQueue("serviceDemoQueue");
-
return serviceDemoQueue;
-
-
}
-
-
@Bean(name = "jmsListenerContainerFactory")
-
public JmsListenerContainerFactory<DefaultMessageListenerContainer> jmsListenerContainerFactory() {
-
DefaultJmsListenerContainerFactory jmsListenerContainerFactory = new DefaultJmsListenerContainerFactory();
-
jmsListenerContainerFactory.setConnectionFactory(cachingConnectionFactory);
-
return jmsListenerContainerFactory;
-
}
-
- }
點選(此處)摺疊或開啟
-
@Configuration
-
public class MongoElConfig {
-
-
@Autowired
-
@Qualifier("mongoDbFactory")
-
private MongoDbFactory mongoDbFactory;
-
-
@Autowired
-
@Qualifier("mongoConverter")
-
private MongoConverter mongoConverter;
-
-
@Bean(name = "mongoTemplate")
-
public MongoTemplate mongoTemplate() {
-
MongoTemplate mongoTemplate = new MongoTemplate(mongoDbFactory, mongoConverter);
-
return mongoTemplate;
-
-
}
-
- }
點選(此處)摺疊或開啟
-
@Configuration
-
@PropertySource("classpath:systemConfig.properties")
-
public class SystemElConfig {
-
-
@Value("${mail.host}")
-
private String mailHost;
-
-
@Autowired
-
private Environment environment;
-
-
@Bean
-
public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
-
return new PropertySourcesPlaceholderConfigurer();
-
}
-
-
/**
-
* @return the mailHost
-
*/
-
public String getMailHost() {
-
return mailHost;
-
}
-
-
/**
-
* @param mailHost
-
* the mailHost to set
-
*/
-
public void setMailHost(String mailHost) {
-
this.mailHost = mailHost;
-
}
-
- }
點選(此處)摺疊或開啟
-
@Configuration
-
@PropertySource("classpath:systemConfig.properties")
-
public class SystemElConfig {
-
-
@Value("${mail.host}")
-
private String mailHost;
-
-
@Autowired
-
private Environment environment;
-
-
@Bean
-
public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
-
return new PropertySourcesPlaceholderConfigurer();
-
}
-
-
/**
-
* @return the mailHost
-
*/
-
public String getMailHost() {
-
return mailHost;
-
}
-
-
/**
-
* @param mailHost
-
* the mailHost to set
-
*/
-
public void setMailHost(String mailHost) {
-
this.mailHost = mailHost;
-
}
-
- }
點選(此處)摺疊或開啟
-
@Configuration
-
public class TransactionManagerElConfig {
-
-
@Autowired
-
@Qualifier("platformTomcat")
-
private DataSource platformTomcat;
-
-
@Autowired
-
@Qualifier("platformReadTomcat")
-
private DataSource platformReadTomcat;
-
-
@Bean(name = "jdbcTemplate")
-
public JdbcTemplate jdbcTemplate() {
-
JdbcTemplate jdbcTemplate = new JdbcTemplate();
-
jdbcTemplate.setDataSource(platformTomcat);
-
return jdbcTemplate;
-
}
-
-
@Bean(name = "jdbcReadTemplate")
-
public JdbcTemplate jdbcReadTemplate() {
-
JdbcTemplate jdbcReadTemplate = new JdbcTemplate();
-
jdbcReadTemplate.setDataSource(platformReadTomcat);
-
return jdbcReadTemplate;
-
}
-
-
@Bean(name = "transactionManager")
-
public DataSourceTransactionManager transactionManager() {
-
DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
-
transactionManager.setDataSource(platformTomcat);
-
return transactionManager;
-
}
-
- }
四:Demo
點選(此處)摺疊或開啟
-
public class App {
-
public static void main(String[] args) {
-
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
-
-
System.out.println(null == context.getBean("platformTomcat"));
-
System.out.println(null == context.getBean("transactionManager"));
-
System.out.println(null == context.getBean("redisTemplate"));
-
System.out.println(null == context.getBean("mongoTemplate"));
-
System.out.println(null == context.getBean("jmsTemplate"));
-
-
System.out.println(context.getBean(SystemElConfig.class).getMailHost());
-
-
context.getBean(ServiceDemo.class).print();
-
-
}
- }
點選(此處)摺疊或開啟
-
@Service
-
public class ServiceDemo {
-
-
@Value("#{systemElConfig.mailHost}")
-
private String mailHost;
-
-
public void print() {
-
System.out.println("=======" + mailHost);
-
}
-
- }
點選(此處)摺疊或開啟
-
@Service
-
public class ServiceDemo {
-
-
@Value("#{systemElConfig.mailHost}")
-
private String mailHost;
-
-
public void print() {
-
System.out.println("=======" + mailHost);
-
}
-
- }
點選(此處)摺疊或開啟
-
@Service
-
public class ServiceDemo {
-
-
@Value("#{systemElConfig.mailHost}")
-
private String mailHost;
-
-
public void print() {
-
System.out.println("=======" + mailHost);
-
}
-
- }
點選(此處)摺疊或開啟
-
@Component
-
@EnableJms
-
public class ServiceDemoListener extends AbstractListener {
-
-
@JmsListener(containerFactory = "jmsListenerContainerFactory", destination = "serviceDemoQueue")
-
public void onMessage(Message message) {
-
-
try {
-
if (message instanceof ObjectMessage) {
-
-
}
-
}
-
catch (Exception e) {
-
logger.error("訊息處理異常", e);
-
}
-
}
-
- }
點選(此處)摺疊或開啟
-
@Component
-
@EnableJms
-
public class ServiceDemoListener extends AbstractListener {
-
-
@JmsListener(containerFactory = "jmsListenerContainerFactory", destination = "serviceDemoQueue")
-
public void onMessage(Message message) {
-
-
try {
-
if (message instanceof ObjectMessage) {
-
-
}
-
}
-
catch (Exception e) {
-
logger.error("訊息處理異常", e);
-
}
-
}
-
- }
點選(此處)摺疊或開啟
-
@Component
-
public class ServiceDemoSchedule {
-
-
@Autowired
-
private GmcSmsInfoBo gmcSmsInfoBo;
-
-
@Scheduled(cron = "0 0/1 * * * ?")
-
public void work() {
-
System.out.println("========Good byte!!=======");
-
-
GmcSmsInfo smsInfo = new GmcSmsInfo();
-
smsInfo.setChannel("test");
-
smsInfo.setContent("test");
-
smsInfo.setStatus("01");
-
smsInfo.setCreateDate(Calendar.getInstance().getTime());
-
smsInfo.setMobile("99999999999");
-
-
try {
-
gmcSmsInfoBo.add(smsInfo);
-
-
System.out.println(null==gmcSmsInfoBo.query(11386));
-
System.out.println(null==gmcSmsInfoBo.query(11401));
-
}
-
catch (BusinessServiceException e) {
-
e.printStackTrace();
-
}
-
-
}
-
- }
點選(此處)摺疊或開啟
-
@Service("gmcSmsInfoBo")
-
public class GmcSmsInfoBo extends AbstractBusinessObject {
-
-
@Autowired
-
private GmcSmsInfoDAO gmcSmsInfoDaoImpl;
-
-
@CachePut(value = "GmcSmsInfoCache", key = "'GmcSmsInfo_'+#result.smsId")
-
@Transactional(rollbackFor={Exception.class,RuntimeException.class})
-
public GmcSmsInfo add(GmcSmsInfo smsInfo) throws BusinessServiceException {
-
System.out.println("=============add==========");
-
try {
-
smsInfo.setSmsId(gmcSmsInfoDaoImpl.save(smsInfo));
-
}
-
catch (FrameworkDAOException e) {
-
throw new BusinessServiceException(e);
-
}
-
return smsInfo;
-
}
-
-
@Cacheable(value="GmcSmsInfoCache",key="'GmcSmsInfo_'+#smsId")
-
public GmcSmsInfo query(Integer smsId) throws BusinessServiceException {
-
System.out.println("=============query==========");
-
try {
-
return gmcSmsInfoDaoImpl.findById(GmcSmsInfo.class, smsId);
-
}
-
catch (Exception e) {
-
throw new BusinessServiceException(e);
-
}
-
}
-
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28624388/viewspace-2136648/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- spring boot使用註解的方式整合mybaitsSpring BootAI
- Spring(三)——註解方式(Annotation)Spring
- Spring整合Mybatis方式一 - 常規整合 - 註冊對映器SpringMyBatis
- Spring系列之新註解配置+Spring整合junit+註解注入Spring
- Spring AOP整合redis(註解方式) 實現快取統一管理SpringRedis快取
- Spring IOC容器-註解的方式Spring
- Spring 註解方式實現的AOPSpring
- spring通過註解註冊bean的方式+spring生命週期SpringBean
- Spring 使用註解方式進行事物管理Spring
- SpringBoot+Dubbo的註解方式整合例項Spring Boot
- Spring中基於註解方式的AOP操作Spring
- Spring IOC容器-註解的方式【更簡化】Spring
- Spring定時器的配置(註解+xml)方式Spring定時器XML
- Spring(三):IoC容器裝配Bean(xml配置方式和註解方式)SpringBeanXML
- Spring學習筆記2(IOC註解方式&AOP)Spring筆記
- Spring整合Mybatis方式二 - SqlSessionTemplateSpringMyBatisSQLSession
- Spring 實現策略模式--自定義註解方式解耦if...elseSpring模式解耦
- spring-AOP(二)實現原理之AspectJ註解方式Spring
- Spring註解Spring
- 【Spring註解】事務註解@TransactionalSpring
- Redis詳解 - SpringBoot整合Redis,RedisTemplate和註解兩種方式的使用RedisSpring Boot
- Spring註解詳解Spring
- 從Hessian RPC 註解方式看Spring依賴注入RPCSpring依賴注入
- Spring mvc+struts2+Hibernate整合例項程式碼(註解詳解)SpringMVC
- Spring5:@Autowired註解、@Resource註解和@Service註解Spring
- Spring常用註解Spring
- Spring註解大全Spring
- Spring boot註解Spring Boot
- Spring新註解Spring
- spring註解2Spring
- Spring 各種註解備註Spring
- spring註冊bean的幾種方式SpringBean
- MyBatis 與 SpringBoot 整合:註解和xml兩種使用方式介紹MyBatisSpring BootXML
- SPRING-spring註解整理Spring
- 基於註解的方式使用spring-integration-redis分散式鎖SpringRedis分散式
- Spring 註解程式設計之模式註解Spring程式設計模式
- 搞懂分散式技術14:Spring Boot使用註解整合Redis快取分散式Spring BootRedis快取
- spring和ehcache整合,實現基於註解的快取實現Spring快取