基于空maven项目,在pom.xml中添加依赖到Spring
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>...</dependency>
</dependencies>
Spring Config资源文件载入:resources中new-XML Configuration File-Spring Config
bean设置Spring持久层对象
<bean id="userDao" class="com.CakeCN.dao.impl.UserDaoImpl"></bean>
获取Spring容器中的对象
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) app.getBean("userDao");
userDao.save();
bean注入内部属性(对象)set方式
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
<bean id="userService" class="com.CakeCN.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
bean注入内部属性(对象)带参构造方式
private UserDao userDao;
public UserServiceImpl(UserDao userDao) {
this.userDao = userDao;
}
<bean id="userService" class="com.CakeCN.service.impl.UserServiceImpl">
<constructor-arg name="userDao" ref="userDao"/>
</bean>
以上xml标签中,name都是jave代码中的类/接口名字,首字母小写,ref都是Spring容器中的bean的id
表示引用
bean注入内部属性(普通属性)set方式
<bean id="userService" class="com.CakeCN.service.impl.UserServiceImpl">
<property name="username" value="Cake"></property>
</bean>
bean注入内部属性(复杂属性,List|Map等)set方式
<bean id="userService" class="com.CakeCN.service.impl.UserServiceImpl">
<property name="myArray" >
<list>
<value>aaa</value>
<value>bbb</value>
</list>
</property>
<property name="myMap" >
<map>
<entry key="key_1" value="value_1"></entry>
<entry key="key_2" value="value_2"></entry>
<entry key="key_3" value-ref="value_ref_bean_id"></entry>
</map>
</property>
</bean>
分模块Spring容器
<import resource="applicationContext-submodule.xml"/>
Springc3p0数据源配置,主要还是利用set注入普通内部属性
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mydatabase"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
Sping通过注解代替bean的标签配置和set注入配置
首先配置组件扫描使Spring知道去哪里找注解,这里表示CakeCN包下的所有代码都会被扫描到
<context:component-scan base-package="com.CakeCN"/>
在实现类前面加上@Component("beanId")
代替bean标签
//<bean id="userDao" class="com.CakeCN.dao.impl.UserDaoImpl"></bean>
@Component("userDao")
public class UserDaoImpl implements UserDao {
public void save(){
System.out.println("Save Running...");
}
}
引用式注入类属性,这里引用到Spring中的beanId为userDao的对象,
@Autowired
注解就表示按数据类型从Spring容器寻找对象注入
@Qualifier("userDao")
和前者一起用,就表示按照beanId注入
两者结合,可以替换成@Resource(name="userDao")
//<bean id="userService" class="com.CakeCN.service.impl.UserServiceImpl"></bean>
@Component("userService")
public class UserServiceImpl implements UserService {
//<property name="userDao" ref="userDao"></property>
@Autowired
@Qualifier("userDao")
private UserDao userDao;
普通数据可以通过@Value("something")
注入
比较好的利用此注入的情况是,从配置文件中读取并注入
@Value("${jdbc.driver}")
private String Driver;
注解初始化方法和预销毁方法
@PostConstruct
public void init(){
System.out.println("初始化...");
}
@PreDestroy
public void destory(){
System.out.println("销毁...");
}
这里由于强制关闭容器时来不及执行对象的销毁代码,可以在容器用完后手动关闭app.close()
Spring配置类,不通过xml而是通过类来实现容器的创建
@Configuration//下面该类是Spring的配置类
@ComponentScan("com.CakeCN")//指定组件扫描的包路径
@PropertySource("classpath:jdbc.properties")
public class SpringConfiguration {
@Value("${jdbc.driver}")
private String jdbcDriver;
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.user}")
private String jdbcUser;
@Value("${jdbc.password}")
private String jdbcPassword;
@Bean("dataSource")//以下面的方法的返回值为指定id的bean对象存在Spring容器中
public DataSource getDataSource() throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(jdbcDriver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(jdbcUser);
dataSource.setPassword(jdbcPassword);
return dataSource;
}
}
其中也可以通过加载局部Spring配置类实现容器构建
@Configuration//下面该类是Spring的配置类
@ComponentScan("com.CakeCN")//指定组件扫描的包路径
@Import(DataSourceSpringConfiguration.class)
//导入多个@Import({X1.class, X2.class})
public class SpringConfiguration {
//空的确实是空的
}
@Configuration//下面该类是Spring的局部模块化配置类
@PropertySource("classpath:jdbc.properties")
public class DataSourceSpringConfiguration {
@Value("${jdbc.driver}")
private String jdbcDriver;
@Value("${jdbc.url}")
private String jdbcUrl;
@Value("${jdbc.user}")
private String jdbcUser;
@Value("${jdbc.password}")
private String jdbcPassword;
@Bean("dataSource")//以下面的方法的返回值为指定id的bean对象存在Spring容器中
public DataSource getDataSource() throws Exception{
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass(jdbcDriver);
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUser(jdbcUser);
dataSource.setPassword(jdbcPassword);
return dataSource;
}
}
使用类加载Spring时,获取容器时需要换一个接口
public static void main(String[] args) {
//ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
UserService userService = (UserService) app.getBean("userService");
userService.save();
}