spring 装配bean的混合使用
在现实中,使用XML或者注解各有道理,建议在自己的工程中所开发的类尽量使用注解方式,因为使用它并不困难,甚至可以说更为简单,而对于引入第三方包或者服务的类,尽量使用XML方式,这样的好处是可以尽量对三方包或者服务的细节减少理解,也更加清晰和明朗。
Spring同时支持这两种形式的装配,所以可以自由选择,只是无论采用XML还是注解方式的装配都是将Bean装配到Spring IoC容器中,这样就可以通过Spring IoC容器去管理各类资源了。
spring-data.xml:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.ssm.chapter10.annotation"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/springmvc?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true"/> <property name="username" value="root"/> <property name="password" value="123456"/> </bean> </beans>
使用注解@ImportResource,引入spring-data.xml所定义的内容
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.ImportResource; @Configuration // @ComponentScan(basePackages = {"com.ssm.chapter10.annotation"}) @ImportResource({"classpath:ssm/chapter10/spring-dataSource.xml"}) public class ApplicationConfig2 { }
import com.ssm.chapter10.annotation.pojo.Role; import com.ssm.chapter10.annotation.service.RoleDataSourceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @Component public class RoleDataSourceServiceImpl implements RoleDataSourceService { @Autowired // @Qualifier("dataSource2") DataSource dataSource = null; // @Override public Role getRole(Long id) { Connection conn = null; ResultSet rs = null; PreparedStatement ps = null; Role role = null; try { conn = dataSource.getConnection(); ps = conn.prepareStatement("select id, role_name, note from t_role where id = ?"); ps.setLong(1, id); rs = ps.executeQuery(); while (rs.next()) { role = new Role(); role.setId(rs.getLong("id")); role.setRoleName(rs.getString("role_name")); role.setNote(rs.getString("note")); } } catch (SQLException e) { e.printStackTrace(); } finally { /**********close database resources************/ try { rs.close(); ps.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } } return role; } }
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig2.class); RoleDataSourceServiceImpl roleDataSourceServiceImpl = context.getBean(RoleDataSourceServiceImpl.class); Role role = roleDataSourceServiceImpl.getRole(1L); System.out.println(role.toString()); context.close();