Spring注解开发

<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"> <!--注解支持-->
<context:annotation-config/> </beans>

bean注册到Spring容器中

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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd"> <!--注解支持-->
<context:annotation-config/>
<!--指定要扫描的包,包下的注解就会生效-->
<context:component-scan base-package="com.qing"/> </beans>

实体类添加注解@Component

package com.qing.dao;

import org.springframework.stereotype.Component;

@Component
public class User {
private String name;
}
import com.qing.dao.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}


User实体类被Spring管理,通过@Component注解注册到Spring容器中

属性注入

属性添加注解@Value("张三丰")

package com.qing.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class User {
@Value("张三丰")
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}
import com.qing.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MyTest {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}

@Component的衍生注解

在web开发中,会按照MVC三层架构分层,分别使用注解,但他们的作用和@Component注解是一样的,都是将类注册到spring容器中,只是为了标记分层使用不同的注解

dao层 @Repository

package com.qing.dao;

import org.springframework.stereotype.Repository;

@Repository
public class UserDao {
}

service层 @Service

package com.qing.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
}

controller层 @Controller

package com.qing.controller;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {
}

自动装配

作用域

单例@Scope("singleton") 原型@Scope("prototype")

package com.qing.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; @Component
@Scope("singleton")
//@Scope("prototype")
public class User {
@Value("张三丰")
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

xml与注解区别

@Configuration 不使用Spring的xml配置,完全Java来做

JavaConfig是Spring的一个子项目,在Spring4之后,它成为了一个核心功能

添加配置类

@Configuration代表这是一个配置类

package com.qing.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; // @Configuration代表这是一个配置类,相当于applicationContext.xml
@Configuration
// @ComponentScan 扫描包下注解
@ComponentScan("com.qing.pojo")
// @Import 导入其他配置类
@Import(QingConfig2.class)
public class QingConfig {
}

实体类添加注解

package com.qing.pojo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component
public class User {
@Value("张三丰")
private String name; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
}
}

测试

import com.qing.config.QingConfig;
import com.qing.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class MyTest {
@Test
public void test01(){
// 通过AnnotationConfigApplicationContext获取容器,通过配置类的class加载
ApplicationContext context = new AnnotationConfigApplicationContext(QingConfig.class);
User user = context.getBean("user", User.class);
System.out.println(user.getName());
}
}

040_Spring注解开发的更多相关文章

  1. SpringMVC注解开发初步

    一.(补充)视图解析器---XmlViewResolver 作用:分离配置信息. 在视图解析器---BeanNameViewResolver的基础之上进行扩充,新建一个myView.xml分离信息 在 ...

  2. SpringMVC的注解开发入门

    1.Spring MVC框架简介 支持REST风格的URL 添加更多注解,可完全注解驱动 引入HTTP输入输出转换器(HttpMessageConverter) 和数据转换.格式化.验证框架无缝集成 ...

  3. Struts2框架之-注解开发

    Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action类中每个方法的绑定这是重点,在这里先简单看一下配置文件中的简单配置: <span style=" ...

  4. Java自定义注解开发

    一.背景 最近在自己搞一个项目时,遇到可需要开发自定义注解的需求,对于没有怎么关注这些java新特性的来说,比较尴尬,索性就拿出一些时间,来进行研究下自定义注解开发的步骤以及使用方式.今天在这里记下, ...

  5. Annotation(四)——Struts2注解开发

    Hibernate和Spring框架的开发前边总结了,这次看一下流行的MVC流程框架Struts2的注解开发吧.Struts2主要解决了从JSP到Action上的流程管理,如何进行Uri和action ...

  6. Annotation(三)——Spring注解开发

    Spring框架的核心功能IoC(Inversion of Control),也就是通过Spring容器进行对象的管理,以及对象之间组合关系的映射.通常情况下我们会在xml配置文件中进行action, ...

  7. Annotation(一)——注解开发介绍

    <p>在编程中,一直强调的一点就是注释的编写,注释的规范等等.尤其是越是核心,程序越复杂,逻辑越多的清空下,注释的编写对我们以后的阅读代码,维护软件起着至关重要的作用.一款软件有着好的注释 ...

  8. spring注解开发中常用注解以及简单配置

    一.spring注解开发中常用注解以及简单配置 1.为什么要用注解开发:spring的核心是Ioc容器和Aop,对于传统的Ioc编程来说我们需要在spring的配置文件中邪大量的bean来向sprin ...

  9. 使用Java注解开发自动生成SQL

    使用注解开发的好处就是减少配置文件的使用.在实际过程中,随着项目越来越复杂,功能越来越多,会产生非常多的配置文件.但是,当配置文件过多,实际维护过程中产生的问题就不容易定位,这样就会徒劳的增加工作量. ...

随机推荐

  1. ssh-正向与反向代理

    常用参数 栗子 实战 常用参数 -N 告诉SSH客户端,这个连接不需要执行任何命令.仅仅做端口转发 -C 表示压缩数据传输 -f 告诉SSH客户端在后台运行 -q Quiet mode. 安静模式,忽 ...

  2. [心得体会]SpringMVC源码分析

    1. SpringMVC (1) springmvc 是什么? 前端控制器, 主要控制前端请求分配请求任务到service层获取数据后反馈到springmvc的view层进行包装返回给tomcat, ...

  3. mysql导入脚本

    #登陆 mysql -u root -p #创建数据库 CREATE DATABASE `gps` CHARACTER SET utf8 COLLATE utf8_general_ci; #选择数据库 ...

  4. mysql某建表语句

    CREATE TABLE `product_info`( `product_id` VARCHAR(32) NOT NULL COMMENT '主键', `product_name` VARCHAR( ...

  5. 淘宝的sign参数js逆向

    前言:现在网站都有很强的反爬机制,都是非常常见的是用js前端加密参数,所以不得不去分析和逆向js混淆后的代码 一. 打开天猫或淘宝,shift+ctrl+F12全局搜索sign参数. 这里发现很多地方 ...

  6. buu Youngter-drive

    一.查壳,发现是upx的壳,用自解压方式,脱下壳 二.之后发现打不开了,应该是要修复,不想修复了,直接拖入ida 找到关键函数,中间发生一点小插曲,发现堆栈不平衡,然后导致F5反编译失败,百度了下是A ...

  7. 题解 AtCoder Beginner Contest 168

    小兔的话 欢迎大家在评论区留言哦~ AtCoder Beginner Contest 168 A - ∴ (Therefore) B - ... (Triple Dots) C - : (Colon) ...

  8. python链接postgresql

    #需要安装的库 sudo apt-get install build-dep python-psycopg2 pip install psycopg2 #!/usr/bin/python # -*- ...

  9. Python使用笔记001

    一.Pycharm小技巧 1.pycharm创建项目时,选择Python环境,不使用默认的虚拟环境 2.如何在pycharm中查看python版本 Files--Settings--Project I ...

  10. MySQL字符串操作函数

    使用方法:concat(str1,str2,-)   返回结果为连接参数产生的字符串.如有任何一个参数为NULL ,则返回值为 NULL. mysql> select concat('11',' ...