<?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: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"> <!--基於註解+xml的spring配置--> <!-- 告知spring在建立容器時要掃描的包 --> <context:component-scan base-package="com.lyy"></context:component-scan> </beans>
在須要放到spring容器中的類上加上對應註解,這時可使用註解來配置bean,不須要在配置文件中用bean標籤來配置。java
@Component
,@Controller
,@Service
,@Repository
,這幾個註解加在類上,告知spring把所在的類建立對象放入容器中。mysql
autowired
git
自動按照類型注入。當使用註解注入屬性時, set 方法能夠省略。它只能注入其餘 bean 類型。當有多個類型匹配時,使用要注入的對象變量名稱做爲 bean 的 id,在 spring 容器查找,找到了也能夠注入成功。找不到就報錯spring
Qualifier
sql
在自動按照類型注入的基礎之上,再按照 Bean 的 id 注入。它在給字段注入時不能獨立使用,必須和 @Autowire 一塊兒使用;可是給方法參數注入時,能夠獨立使用。數據庫
屬性:value,指定bean的idapache
Resource
app
直接按照 Bean 的 id 注入。它也只能注入其餘 bean 類型。 屬性:name,指定bean的idmaven
Value
ide
注入基本數據類型和 String 類型數據的 。屬性:value,用於指定值。
使用spring的註解+xml配置來實現一個對帳戶的crud操做,持久層使用commons-dbutils來實現
-- 建立數據庫 CREATE DATABASE spring_demo1; SHOW TABLES; USE spring_demo1; -- 建立帳戶表 CREATE TABLE account( id VARCHAR(64) PRIMARY KEY, `name` VARCHAR(32), money FLOAT ) -- 新增記錄 INSERT INTO account(id,`name`,money) VALUE(MD5(UUID()),'張三',100); SELECT * FROM account
<dependencies> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.16</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.0.2.RELEASE</version> </dependency> <!--mysql驅動--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!--數據庫鏈接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.11</version> </dependency> <!--JDBC工具類庫--> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.6</version> </dependency> </dependencies>
在這個配置文件中開啓對註解的支持,配置鏈接池的bean和QueryRunner的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:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 開啓註解掃描,管理service和dao --> <context:component-scan base-package="com.lyy.service"> </context:component-scan> <context:component-scan base-package="com.lyy.dao"> </context:component-scan> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置鏈接池 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--配置common-dbutils的核心對象--> <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner"> <constructor-arg name="ds" ref="dataSource"/> </bean> </beans>
import lombok.Data; /** * 帳戶實體類 */ @Data public class Account { private String id; private String name; private float money; }
其中dao實現類以下
@Repository(value = "accountDaoImpl2") public class AccountDaoImpl2 implements IAccountDao { @Autowired private DataSource dataSource; @Autowired private QueryRunner queryRunner; @Override public void insert(Account account) { try { String sql="INSERT INTO account(id,`name`,money) VALUE(MD5(UUID()),?,?)"; Object[] params={account.getName(),account.getMoney()}; queryRunner.update(sql,params); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("新增記錄出錯,e"+e.toString()); } } @Override public List<Account> findAll() { try { String sql="SELECT * FROM account"; List list = (List) queryRunner.query(sql, new BeanListHandler(Account.class)); return list; } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("查詢所有記錄出錯,e"+e.toString()); } } @Override public Account findById(String id) { try { String sql="SELECT * FROM account WHERE id=?"; Account account = queryRunner.query(sql, new BeanHandler<>(Account.class), id); return account; } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("查詢指定記錄出錯,e"+e.toString()); } } @Override public void delete(String id) { try { String sql="DELETE FROM account WHERE id=?"; queryRunner.update(sql,id); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException("刪除指定記錄出錯,e"+e.toString()); } } }
而後在service實現類中用Autowired注入這個對象,就能夠調用dao的方法
@Autowired @Qualifier(value = "accountDaoImpl2") private IAccountDao accountDao;
從spring容器中獲取service對象,並執行對應的方法
@Test public void testFindAll(){ ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml"); IAccountService accountService = app.getBean(IAccountService.class); List<Account> list = accountService.findAll(); System.out.println(Arrays.toString(list.toArray())); }
註解配置和xml配置的過程是同樣的,都是先把bean放入容器,放入的過程當中可能會涉及到依賴注入的問題,用註解實現依賴注入主要用到兩個註解:@Autowired和@Value
示例工程地址
示例工程地址