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

Spring注解开发

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

Spring注解开发

注解:给程序看的代码,程序看到后会执行不同的操作

在使用Spring注解开发之前,你需要理解Spring IoC并且会简单使用它:Spring ioC的简单理解与使用_weixin_45506581的博客-CSDN博客

前提条件:一个 web-maven项目

需要加入的依赖 pom.xml:




  4.0.0

  org.example
  Spring_Test
  1.0-SNAPSHOT
  war

  Spring_Test Maven Webapp
  http://www.example.com

  
    UTF-8
    1.7
    1.7
  

  
    
      junit
      junit
      4.13.2
      test
    


    
      javax.servlet
      servlet-api
      2.5
      provided
    

    
      javax.servlet
      javax.servlet-api
      3.1.0
      provided
    

    
      javax.servlet
      jstl
      1.2
    

    
      mysql
      mysql-connector-java
      5.1.49
    

    
      org.mybatis
      mybatis
      3.2.8
    

    
      cglib
      cglib
      3.3.0
    

    
      org.mybatis.generator
      mybatis-generator-core
      1.3.6
      test
    

    
      org.mybatis
      mybatis-spring
      1.2.2
    
    
      org.springframework
      spring-webmvc
      5.1.9.RELEASE
    
    
      org.springframework
      spring-jdbc
      5.1.9.RELEASE
    

    
      org.projectlombok
      lombok
      1.18.12
      provided
    

    
      com.alibaba
      druid
      1.0.14
    
  

  
    Spring_Test
    
      
        
          org.apache.maven.plugins
          maven-compiler-plugin
          3.1
          
            1.8
            1.8
          
        

        
          org.mybatis.generator
          mybatis-generator-maven-plugin
          1.3.6
        
      
    
  

注解分类

        创建对象注解:@Component(通用注解),@Controller(用于视图层),@Service(用于事务处理层),@Repository(用于数据库交互层) //一般写于类上方

        属性注入注解:@Autowired(自动注入),@Qualifier(按名称 id 注入),@Resource(相当于自动注入与名称注入的结合体),@Value(为Spring对象的属性注入基本类型数据与String类型数据)  //一般写于属性上方

其他注解:@Scope(用于配置修饰的类为单例还是多例,默认为单例),@Lazy(懒加载,用于修饰类,被修饰的类只有用到时才会被加载)

怎么使用?

在需要被Spring管理的类上写上注解,如

@Component //相当于在 application.xml 中配置一个 
public void user{

目录结构:

关键代码如下:

 application.xml 代码:





        

 实体类 

   user:       

package com.wanshi.pojo;

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

@Component
public class user {

    private String id;
    private String username;
    private String userpassword;
    private String realname;

    @Autowired
    private role role;

    static{
        System.out.println("user对象创建了");
    }

    public String getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserpassword() {
        return userpassword;
    }

    public void setUserpassword(String userpassword) {
        this.userpassword = userpassword;
    }

    public String getRealname() {
        return realname;
    }

    public void setRealname(String realname) {
        this.realname = realname;
    }
}

   role:

package com.wanshi.pojo;

import org.springframework.stereotype.Component;

@Component
public class role {

    private int id;
    private String role_name;

    static {
        System.out.println("role对象创建了");
    }

    public int getId() {
        return id;
    }

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

    public String getRole_name() {
        return role_name;
    }

    public void setRole_name(String role_name) {
        this.role_name = role_name;
    }
}

   userService:

package com.wanshi.service;

import com.wanshi.pojo.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class userService {

    static{
        System.out.println("userService对象创建了");
    }

    @Autowired
    private user user;


}

   Test(单元测试类):

package com.wanshi.test;

import com.wanshi.service.userService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;

public class Test {


    @org.junit.Test
    public void Test() throws IOException {
//        初始化工厂对象,用于获取Spring容器中的bean对象
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("application.xml");
//        打印从Spring容器中拿到的userService对象
        userService userservice= (userService) applicationContext.getBean("userService");
    }

}

 运行结果:

 运行方式:

       1.创建ApplicationContext对象时会加载application.xml配置文件,开启包扫描,将有注解的类交由Spring管理

        2.获取userService的bean对象时,因它当中有@Autowired的user类,所以会自动注入到user属性中,当user类被Spring实例化时,因它当中又有@Autowired的role类,所以也会注入role的bean对象到user的role属性

        3.只获取一个userService对象,就一次性获得了它所依赖的其他对象属性,而不用再一个一个new了

大家是不是感觉很方便呢?

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/397290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

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

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