使用 spring 的 IoC 的实现账户的CRUD(2)双层实现+注解开发

这篇具有很好参考价值的文章主要介绍了使用 spring 的 IoC 的实现账户的CRUD(2)双层实现+注解开发。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

在http://t.csdn.cn/yucl4的基础上进行注解开发
【分析】
使用 spring 的 IoC 的实现账户的CRUD(2)双层实现+注解开发

  • 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的容器中找到
使用 spring 的 IoC 的实现账户的CRUD(2)双层实现+注解开发
@Resource

  • 不属于spring的注解,属于java的注解
  • 默认按照名字去找
  • 有两个属性name和type
  • 如果同时指定name和type,则会找到一个唯一的值
  • 如果只指定name,那么会按照name找,找不到报异常
  • 如果只指定type,那么先按照type找,找不到异常,找到多个,根据字段名匹配取

使用 spring 的 IoC 的实现账户的CRUD(2)双层实现+注解开发
使用 spring 的 IoC 的实现账户的CRUD(2)双层实现+注解开发
如何进行完全注解开发——完全不需要.xml文件

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模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Spring篇】IOC/DI注解开发

    🍓系列专栏:Spring系列专栏 🍉个人主页:个人主页 目录 一、IOC/DI注解开发 1.注解开发定义bean  2.纯注解开发模式 1.思路分析 2.实现步骤 3.注解开发bean作用范围与生命周期管理 1.环境准备 2.Bean的作用范围 3.Bean的生命周期 4.注解开发依赖注入 1.环境准备 2.注解实现按照类型注入

    2024年02月03日
    浏览(63)
  • Spring学习笔记之Spring IoC注解式开发

    注解的存在主要是为了简化XML的配置。Spring6倡导全注解开发 注解怎么定义,注解中的属性怎么定义? 注解怎么使用 通过反射机制怎么读取注解 注解怎么定义,注解中的属性怎么定义? 以上是自定义了一个注解:Component 该注解上面修饰的注解包括:Target注解和Retention注解,

    2024年02月12日
    浏览(32)
  • 【Spring教程九】Spring框架实战:全面深入详解IOC/DI注解开发

    欢迎大家回到《 Java教程之Spring30天快速入门》,本教程所有示例均基于Maven实现,如果您对Maven还很陌生,请移步本人的博文《 如何在windows11下安装Maven并配置以及 IDEA配置Maven环境》,本文的上一篇为《 IOC/DI配置管理第三方bean 加载properties文件》。 Spring的IOC/DI对应的配置开

    2024年02月03日
    浏览(37)
  • javaee spring 用注解的方式实现ioc

    spring核心依赖 spring配置文件

    2024年02月10日
    浏览(35)
  • 【Spring教程十】Spring框架实战:全面深入详解IOC/DI之--纯注解开发模式下的依赖注入&&注解读取properties配置文件

    欢迎大家回到《 Java教程之Spring30天快速入门》,本教程所有示例均基于Maven实现,如果您对Maven还很陌生,请移步本人的博文《 如何在windows11下安装Maven并配置以及 IDEA配置Maven环境》,本文的上一篇为《 全面深入详解IOC/DI注解开发》 Spring为了使用注解简化开发,并没有提供

    2024年02月04日
    浏览(44)
  • 【Spring教程11】Spring框架实战:IOC/DI注解开发管理第三方bean的全面深入详解

    欢迎大家回到《 Java教程之Spring30天快速入门》,本教程所有示例均基于Maven实现,如果您对Maven还很陌生,请移步本人的博文《 如何在windows11下安装Maven并配置以及 IDEA配置Maven环境》,本文的上一篇为《 纯注解开发模式下的依赖注入和读取properties配置文件》 前面定义bean的时

    2024年02月04日
    浏览(42)
  • 全面掌握Spring框架:深入解析IOC、AOP、事务管理与注解使用

    探索Spring框架的深层次知识,包括Spring IOC容器的初始化流程、AOP的实现机制、事务管理的细节、循环依赖问题的处理、条件注解的应用、JavaConfig的使用方法、PostProcessor的角色、@Autowired和@Value注解的高级应用,以及${}与#{}的区别。

    2024年03月13日
    浏览(182)
  • Spring02-Spring注解的使用、基于注解的IOC、纯注解配置、整合Junit、AOP入门、基于配置文件的AOP、切入点表达式、基于配置的文件环绕通知

    学习基于注解的 IOC 配置,即注解配置 和 XML 配置要实现的功能都是一样的,都是要降低程序间的耦合。只是配置的形式不一样。 关于实际的开发中到底使用xml还是注解,每家公司有着不同的使用习惯 , 所以这两种配置方式我们都需要掌握。 把 Spring 的 xml 配置内容改为使用

    2024年02月03日
    浏览(63)
  • 129.【Spring 注解_IOC】

    (1).无注解注入方式 在pom文件中加入spring-context依赖: xml文件和注解的包 定义一个实体类 在resource目录下的beans.xml配置文件中通过 bean /bean标签注入类实例 获取容器中通过配置文件注入的实例对象 (2).注解注入方式 1.MyConfig.java 2.Main.java (1).无注解扫描方式 只要我们加上 @Config

    2024年02月12日
    浏览(28)
  • Spring IOC相关注解运用——上篇

    目录 前言 一、@Component 二、@Repository、@Service、@Controller 三、@Scope 四、@Autowired 五、@Qualifier 六、@Value 1. 直接设置固定的属性值 2. 获取配置文件中的属性值 3. 测试结果 往期专栏文章相关导读  1. Maven系列专栏文章 2. Mybatis系列专栏文章 3. Spring系列专栏文章         注解配

    2024年02月02日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包