标签:instance sep mysq 隐式 添加 classes 属性 mysql 实现
1、Spring配置的可选方案
2、自动化装配bean
Spring从两个角度来实现自动化装配:
2.1、创建可被发现的bean——@Component(在POJO实现类上添加@Component注解);
2.2、启用组件扫描——@ComponentScan
@Configuration @ComponentScan public class DemoConfig { }
<beans ... >
<context:component-scan base-package="" />
</beans>
2.3、为组件扫描的bean命名
2.4、设置组件扫描的基础包
2.5、通过为bean添加注解实现自动装配——@Autowired。@Autowired = @inject
3、通过Java代码装配bean
@Configuration // 使用@Autowired声明该类为配置类 public class CDPlayerConfig { @Bean // 默认情况下bean的ID与带有@Bean注解的方法名相同
public CompactDisc sgtPeppers() {
return new SgtPeppers();
}
@Bean(name="beanName")
public CompactDisc sgtPeppers2() {
return new SgtPeppers();
}
@Bean
public CDPlayer cdPlayer(CompactDisc compactDisc) {
return new CDPlayer(compactDisc);
}
}
4、通过XML装配bean
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.10.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.10.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.10.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.10.xsd"> <!-- 配置数据源 --> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- 扫描Mybatist配置文件 --> <bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactory"> <property name="dataSource" ref="dataSource" /> <property name="mapperLocations" value="classpath:mysql/*.xml"/> </bean> <!-- Mapper扫描配置 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zhux.user.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> </beans>
5、导入和混合配置
5.1、在JavaConfig中引用XML配置;
5.2、在XML配置中引用JavaConfig。
标签:instance sep mysq 隐式 添加 classes 属性 mysql 实现
原文地址:http://www.cnblogs.com/zhuxiong/p/7647605.html