首页 > 其他分享 >Spring17_注解开发7

Spring17_注解开发7

时间:2023-05-04 18:55:26浏览次数:49  
标签:userDao Spring17 springframework annotation 开发 import 注解 public

一、Spring原始注解

 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文 件可以简化配置,提高开发效率。

 Spring原始注解主要是替代<Bean>的配置

 

 注入的3个注解用来替代property注入的。

 注意:使用注解进行开发时,需要在applicationContext.xml中配置组件扫描,作用是指定哪个包及其子包下的Bean需要进行扫描以便识别使用注解配置的类、字段和方法。

<!--注解的组件扫描-->
<context:component-scan base-package="com.itheima"></context:component-scan>
 

 1. 完善测试环境

    

    

    

   执行main,可以看到控制台输出:save running ...,表明环境OK

 2. 注解开发实现

     

  

  执行UserController,输出成功

 3. 实例化Bean的注解

  上面使用的是Component注解,虽然能用但是不能确定是在哪层,所以建议使用Controller、Service、Repository注解去分清web层、service层、dao层

@Service("userService")
public class UserServiceImpl implements UserService {
    ... ...
}
@Repository("userDao")
public class UserDaoImpl implements UserDao {
    ... ...
}

 4. 注入对象的注解

  使用@Autowired或者@Autowired+@Qulifier或者@Resource进行userDao的注入

    //<property name="userDao" ref="userDao"></property>
    @Autowired //按照数据类型从Spring容器中进行匹配的,可单独使用
    private UserDao userDao;

//    public void setUserDao(UserDao userDao) {
//        this.userDao = userDao;
//    }
    //<property name="userDao" ref="userDao"></property>
    @Autowired //按照数据类型从Spring容器中进行匹配的,可单独使用
    @Qualifier("userDao") //ref值,按照id值从容器中进行匹配的,但是注意:@Qualifier需要结合@Autowired一起使用
    private UserDao userDao;

//    public void setUserDao(UserDao userDao) {
//        this.userDao = userDao;
//    }
    //<property name="userDao" ref="userDao"></property>
    @Resource(name="userDao") //name=id值,相当于@Autowired+@Qualifier
    private UserDao userDao;

//    public void setUserDao(UserDao userDao) {
//        this.userDao = userDao;
//    }

 4. 注入普通属性的注解

package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

//<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
@Service("userService")
public class UserServiceImpl implements UserService {

    @Value("itcast")
    private String name;

    @Value("${jdbc.driver}")
    private String driver;
    
    //<property name="userDao" ref="userDao"></property>
    @Resource(name="userDao") //name=id值,相当于@Autowired+@Qualifier
    private UserDao userDao;

    public void save() {
        System.out.println(name);
        System.out.println(driver);
        userDao.save();
    }

}

  执行UserController,检查控制台输出

  

 5. Scope注解

  相当于bean标签里的scope,表示对象是单例,还是多例

//<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
@Service("userService")
//@Scope("prototype")
@Scope("singleton")
public class UserServiceImpl implements UserService {
    ... ...
}

 6. 初始化方法、销毁方法的注解

  相当于bean标签中的init-method、destroy-method

package com.itheima.service.impl;

import com.itheima.dao.UserDao;
import com.itheima.service.UserService;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;

//<bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
@Service("userService")
@Scope("singleton")
public class UserServiceImpl implements UserService {

    @Value("itcast")
    private String name;

    @Value("${jdbc.driver}")
    private String driver;

    //<property name="userDao" ref="userDao"></property>
    @Resource(name="userDao") //name=id值,相当于@Autowired+@Qualifier
    private UserDao userDao;

    public void save() {
        System.out.println(name);
        System.out.println(driver);
        userDao.save();
    }

    @PostConstruct
    public void init(){
        System.out.println("init...");
    }

    @PreDestroy
    public void destroy(){
        System.out.println("destroy...");
    }
}
package com.itheima.web;

import com.itheima.service.UserService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserController {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userService = app.getBean(UserService.class);
        userService.save();
        app.close();
    }
}

  执行UserController,检查控制台输出

  

二、Spring新注解

 使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

标签:userDao,Spring17,springframework,annotation,开发,import,注解,public
From: https://www.cnblogs.com/ajing2018/p/17372210.html

相关文章

  • 面试题——python后端开发
    Python和Java、PHP、C、C#、C++等其他语言的对比?python语言,是面向对象、直译式计算机程序设计语言,python语法简洁清晰,具有丰富和强大的类库。Python是完全面向对象的语言。函数、模块、数字、字符串都是对象。并且完全支持继承、重载、派生、多继承,有益于增强源代码的复用性......
  • Vue.js 教程:如何使用 Mock.js 进行前端开发
    Mock.js 是常用的生成随机数据,拦截Ajax请求的JavaScript库。在软件开发的初期,后端API接口可能还没有实现或者还没有完全实现。在这种情况下,用Mock.js可以模拟后端API供前端开发人员调用,从而避免等待后端API的完成。要在Vue项目中使用Mock.js有两个思路:在客户端拦......
  • @enableFeignClients注解的basePackages属性的作用
    basePackages属性是@EnableFeignClients注解的一个可选属性,它用于指定需要扫描的包路径。通过设置该属性,可以告诉Spring在哪些包下查找用@FeignClient注解标记的接口。basePackages中的包可以指定其他模块的包。在多模块的项目中,如果你想要在一个模块中使用另一个模块的......
  • Python网页应用开发神器fac 0.2.6版本重要新功能一览
    fac项目地址:https://github.com/CNFeffery/feffery-antd-components,欢迎star支持大家好我是费老师,距离我的开源Python网页应用通用组件库fac的0.2.0版本发布已过去半个多月的时间,在国内外众多fac用户的反馈建议下,经过高强度的优化和功能更新,目前fac已经更新到0.2.6版本,且......
  • 在开发中,我发现的reactive和ref
    在开发中,我发现了一个问题,就是在element-plus-table组件中使用ref(),定义的数据,在vue文件内部去修改这个响应式数据,任何时候,响应式数据改变,基本上视图也会跟着变化。 但是当我觉得vue文件中script中的代码太长了,将一些功能抽出来,通过引入外部函数的方式去更新响应式数据时,就......
  • Spring17_配置数据源6
    一、数据源(连接池)的作用数据源(连接池)是为提高程序性能而出现的事先实例化数据源,初始化部分连接资源使用连接资源时从数据源中获取使用完毕后将连接资源归还给数据源常见的数据源(连接池):DBCP、C3P0、BoneCP、Druid等二、数据源开发步骤1.导入数据源的坐标和数据......
  • springboot 切面注解方式 记录日志
    1.定义GateOpLogimportjava.lang.annotation.*;/***操作日志记录*@authorcodefulture*/@Target({ElementType.METHOD,ElementType.TYPE})@Retention(RetentionPolicy.RUNTIME)@Documentedpublic@interfaceGateOpLog{/***说明*/Strin......
  • 直播电商平台开发,环形进度条组件
    直播电商平台开发,环形进度条组件 <template> <divclass="content"ref="box">  <svg   :id="idStr"   style="transform:rotate(-90deg)"   :width="width"   :height="width"   xmlns=&......
  • 用手机号码归属地 API 开发的应用推荐
    引言手机号码归属地API是一种提供手机号码归属地信息的接口,通过该接口,可以获取手机号码所属的省份、城市、运营商等信息。它可以帮助企业更好地了解客户,为个性化推荐和精准广告投放提供数据支持。作为一种数据服务,手机号码归属地API在电商、金融、社交、广告等领域得到广泛应用......
  • 直播app开发搭建,图形和短信验证码的自动识别获取
    直播app开发搭建,图形和短信验证码的自动识别获取selenuim操作 driver=webdriver.Chrome()driver.get("https://locvps.wenjingnetwork.com/page.aspx?c=reg")driver.implicitly_wait(10)#设置超时时间driver.find_element_by_name("uname").send_keys()driver.find_element_by......