栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

1.@Component

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

(1)定义

创建对象,等同于功能

(2)创建maven工程 (3)pom

      org.springframework
      spring-context
      5.2.6.RELEASE
    
(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加入组件扫描器




    
    

(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




    
    

(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




    
    

(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




    
    

(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

      org.springframework
      spring-context
      5.2.6.RELEASE
    
(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加入组件扫描器




    
    

(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

      org.springframework
      spring-context
      5.2.6.RELEASE
    
(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加入组件扫描器




    
    

(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

      org.springframework
      spring-context
      5.2.6.RELEASE
    
(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加入组件扫描器




    
    

(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
 
      org.springframework
      spring-context
      5.2.6.RELEASE
    
(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加入组件扫描器




    
    

(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都有自己的分层角色.
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/644208.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号