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. RabbitMq脑裂问题

    现象 部署在阿里云上的2台RabbitMQ主从,访问management页面时出现如下所示的内容: 查看其中一个mq的日志,发现如下内容: 00:06:32.423 [warning] <0.5 ...

  2. Springboot:单元测试@FixMethodOrder注解指定测试方法的执行顺序

    我们在写JUnit测试用例时,有时候需要按照定义顺序执行我们的单元测试方法,比如如在测试数据库相关的用例时候要按照测试插入.查询.删除的顺序测试.如果不按照这个顺序测试可能会出现问题,比如删除方法在前 ...

  3. shiro框架整合ssm框架

    下面我通过一个web的maven项目来讲解如何将shiro整合ssm框架,具体结构如下图 一.引入依赖的jar包 <?xml version="1.0" encoding=& ...

  4. docker进入容器所在虚拟机的指令

    sudo docker inspect -f {{.State.Pid}} 44fc0f0582d9 在拿到该进程PID之后我们就可以使用nsenter命令访问该容器了. $ sudo nsenter ...

  5. XCTF 3rd-GCTF-2017 hackme

    一.查壳的老生常谈了..2分的题目就不多bb了. 二..elf文件,拖入ida,直接查找字符串,找到对应的函数 三.直接分析: 这里讲道理我当时很懵逼,因为进去这个函数后,发现伪码非常复杂,这里困了挺 ...

  6. cke编辑器插入&ZeroWidthSpace占位字符的问题记录

    背景 本博文主要记录在使用cke编辑器时,遇到的一系列的问题 问题1:在执行某些业务操作后,编辑器会偶现在页面头部或者尾部插入&ZeroWidthSpace占位符(编辑器好像就爱干这事~) 解 ...

  7. Vue中watch与computed的区别

    一. 计算属性(computed) 1.计算属性是为了模板中的表达式简洁,易维护,符合用于简单运算的设计初衷.对于运算过于复杂,冗长,且不好维护,因此我们对于复杂的运算应该 使用计算属性的方式去书写. ...

  8. AI 预测蛋白质结构「GitHub 热点速览 v.21.29」

    作者:HelloGitHub-小鱼干 虽然 AI 领域藏龙卧虎,但是本周预测蛋白质结构的 alphafold 一开源出来就刷爆了朋友圈,虽然项目与我无关,但是看着科技进步能探寻到生命机理,吃瓜群众也有 ...

  9. CF466D题解

    思路: 我们首先处理出每个位子需要被多少个区间覆盖才能变成 \(h\) .即 $a_i=h-a_i $ 同时设定 \(b\) 序列为 \(a\) 序列的差分系列 如果 \(b_i==1\) ,很显然, ...

  10. MySql数据库-查询、插入数据时转义函数的使用

    最近在看一部php的基础视频教程,在做案例的时,当通过用户名查询用户信息的时候,先使用了转义函数对客户提交的内容进行过滤之后再交给sql语句进行后续的操作.虽然能看到转义函数本身的作用,但是仍然有一些 ...