Spring框架 步骤 概述与介绍(2)注解注入

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都有自己的分层角色.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值