在http://t.csdn.cn/yucl4的基础上进行注解开发
【分析】
- xml文件其中spring容器中的bean,
- 因此通过注解把这些放到容器中即可
@component:相当xml中的bean的id:
- 如果不指定 value 属性,默认 bean 的 id 是当前类的类名, 首字母小写。
- @Controller @Service @Repository是三个衍生
@Autowired
- 自动按照类型注入。
- 当使用注解注入属性时,setter 方法可以省略
- 它只能注入其他 bean 类型(spring容器中有的)。
- 当有多个类型匹配时,使用要注入的对象变量名称作为 bean 的 id,在 spring 容器查找,找到了也可以注入成功。
- @qualifier:按照id注入,必须与@Autowired搭配
package com.kfm.spring.dao;
import com.kfm.spring.model.Account;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.sql.SQLException;
/**
* @author jkk
*/
@Component("accountdao")
public class IAccountdao implements Accountdao{
@Autowired
private QueryRunner queryRunner;
// public void setQueryRunner(QueryRunner queryRunner){
// this.queryRunner = queryRunner;
// }
@Override
public Account findByid(int id) {
String sql = "select * from account where id = ?";
try {
Account account = queryRunner.query(sql,new BeanHandler<>(Account.class),id);
return account;
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
package com.kfm.spring.service;
import com.kfm.spring.dao.Accountdao;
import com.kfm.spring.model.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @author jkk
*/
@Component("accountservice")
public class IAccountService implements AccounrService{
@Autowired
private Accountdao accountdao;
// public void setAccountdao(Accountdao accountdao){
// this.accountdao = accountdao;
// }
@Override
public Account findByid(int id) {
return accountdao.findByid(id);
}
}
.xml
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
">
<context:component-scan base-package="com.kfm.spring" ></context:component-scan>
<bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner">
<constructor-arg name="ds" ref="datasource"></constructor-arg>
</bean>
<bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/web"></property>
<property name="user" value="root"></property>
<property name="password" value=""></property>
</bean>
</beans>
可以注意到,现在还不是完全的注解开发,只是替换了两个,既然两个用注解替换,那么如何从spring的容器中找到
@Resource
- 不属于spring的注解,属于java的注解
- 默认按照名字去找
- 有两个属性name和type
- 如果同时指定name和type,则会找到一个唯一的值
- 如果只指定name,那么会按照name找,找不到报异常
- 如果只指定type,那么先按照type找,找不到异常,找到多个,根据字段名匹配取
如何进行完全注解开发——完全不需要.xml文件文章来源:https://www.toymoban.com/news/detail-437919.html
package com.kfm.spring.config;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
/**
* @author jkk
*/
@Configuration
@ComponentScan({"com.kfm.spring"})
public class SpringConfig {
@Bean(name = "datasource")
public DataSource getDatasource(){
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
try {
comboPooledDataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql://localhost:3306/web");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("");
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
return comboPooledDataSource;
}
@Bean(name = "queryRunner")
public QueryRunner getQueryRunner(DataSource dataSource){
return new QueryRunner(dataSource);
}
}
测试‘文章来源地址https://www.toymoban.com/news/detail-437919.html
package com.kfm.spring.service;
import com.kfm.spring.config.SpringConfig;
import com.kfm.spring.dao.Accountdao;
import com.kfm.spring.model.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = "classpath:bean.xml")
@ContextConfiguration(classes = SpringConfig.class)
public class IAccountServiceTest {
@Autowired
private AccounrService accounrService;
@Test
public void test1FindByid(){
Account account = accounrService.findByid(1);
System.out.println(account);
}
@Test
public void testFindByid() {
ApplicationContext cx = new AnnotationConfigApplicationContext(SpringConfig.class);
AccounrService accounrService = (AccounrService) cx.getBean("accountservice");
Account account = accounrService.findByid(1);
System.out.println(account);
}
}
到了这里,关于使用 spring 的 IoC 的实现账户的CRUD(2)双层实现+注解开发的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!