在現實中,使用XML或者註解各有道理,建議在本身的工程中所開發的類儘可能使用註解方式,由於使用它並不困難,甚至能夠說更爲簡單,而對於引入第三方包或者服務的類,儘可能使用XML方式,這樣的好處是能夠儘可能對三方包或者服務的細節減小理解,也更加清晰和明朗。
Spring同時支持這兩種形式的裝配,因此能夠自由選擇,只是不管採用XML仍是註解方式的裝配都是將Bean裝配到Spring IoC容器中,這樣就能夠經過Spring IoC容器去管理各種資源了。java
spring-data.xml:mysql
<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所定義的內容spring
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();