通過@Component裝配Bean,但是@Component只能注解在類上,不能注解到方法上。對於Java而言,大部分的開發都需要引入第三方的包(jar文件),而且往往並沒有這些包的源碼,這時候將無法為這些包的類加入@Component注解,讓它們變為開發環境的Bean。你可以使用新類擴展(extends)其包內的類,然后在新類上使用@Component,但是這樣又顯得不倫不類。這個時候Spring給予一個注解@Bean,它可以注解到方法之上,並且將方法返回的對象作為Spring的Bean,存放在IoC容器中。比如我們需要使用DBCP數據源,這個時候要引入關於它的包,然后可以通過代碼清單來裝配數據源的Bean。
代碼清單:通過注解@Bean裝配數據源Bean
import org.apache.commons.dbcp.BasicDataSourceFactory; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; import javax.sql.DataSource; import java.util.Properties; @Component public class AnnotationBean { @Bean(name = "dataSource2") public DataSource getDataSource() { Properties props = new Properties(); props.setProperty("driver", "com.mysql.cj.jdbc.Driver"); props.setProperty("url", "jdbc:mysql://localhost:3306/springmvc?useSSL=false&serverTimezone=Hongkong&characterEncoding=utf-8&autoReconnect=true"); props.setProperty("username", "root"); props.setProperty("password", "123456"); DataSource dataSource = null; try { dataSource = (DataSource) BasicDataSourceFactory.createDataSource(props); System.out.println("getDataSource init"); } catch (Exception e) { e.printStackTrace(); } return dataSource; } }
這樣就能夠裝配一個Bean,當Spring IoC容器掃描它的時候,就會為其生成對應的Bean。這里還配置了@Bean的name選項為dataSource2,這就意味着Spring生成該Bean的時候就會使用dataSource2作為其BeanName。和其他Bean一樣,它也可以通過@Autowired或者@Qualifier等注解注入別的Bean中。
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; } }
spring-dataSource.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"/> </beans>
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ssm/chapter10/spring-dataSource.xml"); RoleDataSourceServiceImpl roleDataSourceServiceImpl = context.getBean(RoleDataSourceServiceImpl.class); Role role = roleDataSourceServiceImpl.getRole(1L); System.out.println(role.toString()); context.close();