Spring框架 步骤 概述与介绍(2)注解注入-程序员宅基地

技术标签: spring  java  后端  

1.@Component

*#本文为上课时实例,篇幅较长,请耐心查阅

(1)定义

创建对象,等同于功能

(2)创建maven工程

(3)pom

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>

(4)entity并加入注解

package cn.kgc.entity;

import org.springframework.stereotype.Component;

@Component(value = "myUser")
public class User   {
    
    private Integer id;
    private String name;

    public Integer getId() {
    
        return id;
    }

    public void setId(Integer id) {
    
        this.id = id;
    }

    public String getName() {
    
        return name;
    }

    public void setName(String name) {
    
        this.name = name;
    }
}

(5)applicationContext.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 
       http://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="cn.kgc.entity"/>
</beans>

(6)测试类TestSpring

package cn.kgc.test;

import cn.kgc.entity.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    
    @Test
    public void testdemo001(){
    
        String conf = "applicationContext.xml";
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
        User myUser = (User) ac.getBean("myUser");
        System.out.println(myUser);
    }
}

(7)拓展

第一种拓展:@Component( “myUser”)
第二种拓展:@Component

2.@Repository

(1)定义

用在持久层上面,方法在dao的实现类上面,表示创建dao对象

(2)核心代码UserMapper.java

public interface UserMapper {
    
    public Integer addUser();
}

(3)核心代码UserMapperImpl.java

@Repository(value = "userMapper")   //在申明XXMapper的bean对象的时候,@Respository中的value不建议省略
public class UserMapperImpl implements UserMapper{
    
    @Override
    public Integer addUser() {
    
        System.out.println("调用持久层方法....");
        return 0;
    }
}

(4)核心代码applicationContext.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 
       http://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="cn.kgc.dao"/>
</beans>

(5)核心代码TestSpring

public class TestSpring {
    
    @Test
    public void testdemo001(){
    
        String conf = "applicationContext.xml";
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
        UserMapper u = (UserMapper) ac.getBean("userMapper");
        System.out.println(u.addUser());
    }
}

3.@Service

(1)定义

用在业务层上面,放在service的实现类上面,表示创建service对象,可以有一些事务功能

(2)核心代码UserService

public interface UserService {
    
    public Integer addUser();
}

(3)核心代码UserServiceImpl

@Service(value = "userService")
public class UserServiceImpl implements UserService{
    
    @Override
    public Integer addUser() {
    
        System.out.println("调用addUserService....");
        return 0;
    }
}

(4)核心代码applicationContext.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 
       http://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="cn.kgc"/>
</beans>

(5)TestSpring

public class TestSpring {
    
    @Test
    public void testdemo001(){
    
        String conf = "applicationContext.xml";
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
        UserService userService = (UserService) ac.getBean("userService");
        System.out.println(userService.addUser());
    }
}

4.@Controller

(1)定义

用在控制器上面,放在控制器上面,创建控制前对象,能够接受用户提交的参数,显示请求的处理结果

(2)核心代码UserController

@Controller(value = "userController")
public class UserController {
    
    public Integer addUser(){
    
        System.out.println("调用userController.....");
        return 0;
    }
}

(3)核心代码applicationContext.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 
       http://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="cn.kgc"/>
</beans>

(4)TestSpring

public class TestSpring {
    
    @Test
    public void testdemo001(){
    
        String conf = "applicationContext.xml";
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
        UserController userController = (UserController) ac.getBean("userController");
        System.out.println(userController.addUser());
    }
}

5.@Value

(1)定义

给简单类型属性对象赋值

(2)创建maven工程

(3)pom

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>

(4)entity并加入注解

package cn.kgc.entity;

import org.springframework.stereotype.Component;

@Component
public class User   {
    
    @Value(value = "1")
    private Integer id;
    @Value(value = "zs")
    private String name;
	//注意:@Value等价于set()方法,无需再定义set()
    public Integer getId() {
    
        return id;
    }

    public String getName() {
    
        return name;
    }
}

(5)applicationContext.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 
       http://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="cn.kgc.entity"/>
</beans>

(6)测试类TestSpring

package cn.kgc.test;

import cn.kgc.entity.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    
    @Test
    public void testdemo001(){
    
        String conf = "applicationContext.xml";
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
        User myUser = (User) ac.getBean("user");
        System.out.println(myUser.getId()+"    "+myUser.getName());
    }
}

6.@Autowired

(1)定义

给引用类型属性赋值(自动装配Bean),使用,默认使用byType自动注入。

(2)创建maven工程

(3)pom

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>

(4.1)entity/User

package cn.kgc.entity;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class User   {
    

    @Value(value = "1")
    private Integer id;
    @Value(value = "zs")
    private String name;
    @Autowired
    private Role role;

    @Autowired
    private SonRole sonRole;

    public SonRole getSonRole() {
    
        return sonRole;
    }

    public Role getRole() {
    
        return role;
    }

    public Integer getId() {
    
        return id;
    }

    public String getName() {
    
        return name;
    }
}

(4.2)entity/Role.

package cn.kgc.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Role {
    
    @Value(value = "99")
    private Integer rId;
    @Value(value = "老师")
    private String rName;

    public Integer getrId() {
    
        return rId;
    }


    public String getrName() {
    
        return rName;
    }
}

(4.3)拓展 entity/SonRole

@Component
public class SonRole extends Role{
    

}

(5)applicationContext.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 
       http://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="cn.kgc.entity"/>
</beans>

(6)测试类TestSpring

package cn.kgc.test;

import cn.kgc.entity.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    
    @Test
    public void testdemo001(){
    
        String conf = "applicationContext.xml";
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
        User myUser = (User) ac.getBean("user");
        System.out.println(myUser.getId()+"    "+myUser.getName()+" "+myUser.getRole().getrName()+" "+myUser.getSonRole().getrName());
    }
}

7.@Resource

(1)定义

给引用类型赋值,Spring提供了对JDK注解@Resource的支持,默认按名称注入,如果按名称注入失败,自动按类型注入

(2)创建maven工程

(3)pom

<dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>

(4.1)entity/User

package cn.kgc.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class User   {
    
    @Value("11")
    private Integer id;
    @Value("zz")
    private String name;

    @Resource
    private Role role;

    public Role getRole() {
    
        return role;
    }

    public Integer getId() {
    
        return id;
    }

    public String getName() {
    
        return name;
    }
}

(4.2)entity/Role

package cn.kgc.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Role {
    
    private Integer rId;
    @Value("李四")
    private String rName;

    public Integer getrId() {
    
        return rId;
    }

    public String getrName() {
    
        return rName;
    }
}

(5)applicationContext.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 
       http://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="cn.kgc.entity"/>
</beans>

(6)测试类TestSpring

public class TestSpring {
    
    @Test
    public void testdemo001(){
    
        String conf = "applicationContext.xml";
        ClassPathXmlApplicationContext ac = 
        new ClassPathXmlApplicationContext(conf);
        User myUser = (User) ac.getBean("user");
        System.out.println(myUser.getId()+"    "+myUser.getName()+" "+myUser.getRole().getrName() );
    }
}

7.2@Resource拓展

(1)创建maven工程

(2)pom

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.2.6.RELEASE</version>
    </dependency>

(3)entity/User

package cn.kgc.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class User   {
    @Value("11")
    private Integer id;
    @Value("zz")
    private String name;

    @Resource
    private Role role33;

    public Role getRole33() {
        return role33;
    }

    public Integer getId() {
        return id;
    }

    public String getName() {
        return name;
    }
}

(4)entity/Role

package cn.kgc.entity;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class Role {
    private Integer rId;
    @Value("李四")
    private String rName;

    public Integer getrId() {
        return rId;
    }

    public String getrName() {
        return rName;
    }
}

(5)applicationContext.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 
       http://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="cn.kgc.entity"/>
</beans>

(6)测试类TestSpring

package cn.kgc.test;

import cn.kgc.entity.User;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestSpring {
    @Test
    public void testdemo001(){
        String conf = "applicationContext.xml";
        ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext(conf);
        User myUser = (User) ac.getBean("user");
        System.out.println(myUser.getId()+"    "+myUser.getName()+" "+myUser.getRole33().getrName() );
    }
}

8.拓展:

@Component和@Repository,@Service,@Controller的异同
同:都可以创建对象
异:@Repository,@Service,@Controller都有自己的分层角色.
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/tmxk1998/article/details/121776072

智能推荐

QT:第四章:布局管理-程序员宅基地

文章浏览阅读835次,点赞11次,收藏7次。给子对象设置父可以通过setParent()获取子对象的父可以通过parent()方法回去父对象的孩子列表children(),返回值为QList

学会通过商品id来获得商品详情_props_name-程序员宅基地

文章浏览阅读475次。请求参数:num_iid=0070517287/000000012389328898。参数说明:num_iid:店铺ID/商品ID。异常示例相关资料错误码解释。_props_name

跳出多重循环_跳出标记循环的优劣-程序员宅基地

文章浏览阅读1k次。用break语句智能跳出本层循环,但是有的时候要求跳出两层或者更多层的循环该如何处理?我想一般有两种办法。第一种办法就是使用goto语句,这样的话可以进行大范围的转移,跳出多少层循环都是可以的。但是众所周知,goto语句被大多数程序员所抵触,因为它违反了结构化?第二种办法,我想就比较麻烦一点,就是在跳出之前设定一个标志(比如说给一个bool型的变量flag赋值),然后在第二层循环中判断,选择跳出第_跳出标记循环的优劣

ipmitool命令详解-程序员宅基地

文章浏览阅读2.9k次,点赞30次,收藏33次。ipmitool命令详解_ipmitool

扩展Server 2022 C盘容量_windows server2022 c盘扩容-程序员宅基地

文章浏览阅读1.1k次,点赞18次,收藏25次。DISKPART> select disk #默认一块磁盘这里为 0,即:select disk 0DISKPART> select partition #即:select partition 4。_windows server2022 c盘扩容

【软件安装】IDM安装并扩展到FireFox和Google Chrome_idm怎么火绒扩展-程序员宅基地

文章浏览阅读7.5k次,点赞4次,收藏7次。最近做毕设,需要用到一个国外的数据集LANL异常检测数据集。本来以为不过是下载个数据集,能有多麻烦,结果自己下载的时候差点没被整吐。当然这也跟我平时不怎么关注那些下载提速用的软件和插件有关。后来同学跟我推荐了IDM这个加速器,下载的时候也耗费了一些神气。因为网上的相关博客很多,但是很多博客只是介绍IDM安装过程的其中一部分。所以尽管看了很多篇博客,还是遇到了一些问题,主要应该是网上大部分都是直接..._idm怎么火绒扩展

随便推点

在OpenCV里实现不规则ROI的选取_opencv自动选中不规则区域-程序员宅基地

文章浏览阅读1k次。有时候处理图像时,需要像画图软件一样,可以点击鼠标来选择不同的区域,这样选择的区域往往是不规则的图像,那么怎么样才可以在OpenCV里实现这样的功能呢?在这里要采用一点技巧,就是图像的像素与白色像素的与关系运算,任何颜色像素与白色像素作与运算都会只有这个像素的颜色,而与黑色像素运算就只剩下黑色。不规则的图形填充可以使用drawContours函数来实现。演示的例子如下:#python 3.7..._opencv自动选中不规则区域

基于STM32的实时心率监测仪设计与实现_stm心率传感器-程序员宅基地

文章浏览阅读375次。此外,我们还需要心率传感器模块来测量心率信号。常用的心率传感器模块有光电传感器和心电传感器,可以根据实际需求选择。心率监测是一项重要的医疗技术,它可以及时监测和记录人体的心率变化,为医疗保健提供有价值的数据。通过该设备,用户可以方便地监测自己的心率,并实时获取心率数据。b. 心率信号处理:对采集到的心率信号进行滤波和放大处理,以提高信号的可靠性和稳定性。a. 心率信号采集:使用心率传感器模块采集心率信号,并将其转换为数字信号。c. 心率计算:通过对处理后的心率信号进行分析和计算,得出实时的心率数值。_stm心率传感器

python基础——while循环语句_python while循环语句-程序员宅基地

文章浏览阅读1.2w次,点赞14次,收藏71次。1.while循环基本使用循环的作用就是让指定的代码重复的执行. while循环最常用的应用场景就是让执行的代码按照指定的次数重复执行需求--打印5遍Hello World思考--如果要求打印100遍怎么办?1.1 while语句基本语法初始条件设置——通常是重复执行的 计数器while条件(判断 计数器 是否达到 目标次数):条件满足时,做的事情A条件满足时,做的事情B条件满足时,做的事情C...........处理条件(计数器+ 1)注意: while_python while循环语句

【Android Studio】解决ERROR: Cause: invalid type code: 2D_android studio cause: invalid type code: b6-程序员宅基地

文章浏览阅读2.5k次。解决ERROR: Cause: invalid type code: 2D自己在新建Kotlin工程文件时遇见了这个错误,通过在build.gradle(Project) 中添加了第三方maven解决了这个错误。 maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' } maven { url "https://jitpack.io" }build.gradle(Project) 代码:/_android studio cause: invalid type code: b6

小程序音视频性能测试_环境搭建_音视频代理 测试方法-程序员宅基地

文章浏览阅读927次。小程序音视频性能测试_环境搭建一.逻辑视图二.环境资源准备测试环境:梳理出所有相关的服务器和涉及到的测试工具,并估算出需要的测试资源量。如:软硬件(外网)作用 数量小程序后台服务器 连接业务支撑数据库,校验小程序的用户等; 1Web代理服务器 消息转发和音频编解码,音视频转发 3Zookeeper服务 Zookeeper处理代理,使多台代理不会重复去会议服务器取流 1Mysql..._音视频代理 测试方法

HDU-2546 饭卡01背包问题_0_10_饭卡01背包-程序员宅基地

文章浏览阅读185次。HDU-2546 01背包问题题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2546题意:这其实就是一个简单地01背包问题关于01背包:有N件物品和一个容量为V的背包。第i件物品的体积是v[i],价值是w[i]。求解将哪些物品装入背包可使价值总和最大。每个物品只能往背包中装最多一次的操作;注意:因为这个题要求我们要使最后的饭卡的钱最小,我..._0_10_饭卡01背包

推荐文章

热门文章

相关标签