还不知道spring配置的先去看这个,不然很多配置不理解
https://blog.csdn.net/tomwildboar/article/details/80913681

1、导包,需要在之前6个包的基础上再导入一个aop包

路径如下
spring-framework-4.2.4.RELEASE\libs
lib目录有以下包

2、导入约束,这个约束是基于入门配置的第二个约束,如果不知道第一个约束怎么导入,去看上面的链接

window->Preference

然后直接OK -> OK 就好了

3、配置约束

3-1 创建applicationContext.xml 并配置第一种约束,不懂得看开头链接。配置好了如下

3-2配置第二个约束

配置成功效果

4、实体创建 (注解基本都在这里了,我都在代码中详细解释了)

4-1 User实体主要实体注解基本都在这里面

package springDemo.bean;import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;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.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;//相当于 <bean name="user" class="springDemo.bean.User" />
@Component("user")
//  @Service("user")     //service层
//  @Controller("user")      //web层
//  @Repository("user")      //dao层//下面三个的Component的意义是一样的,但是为了使用者更好的阅读,一般建议采用下面的三个//来指定对象的作用域
@Scope(scopeName="singleton")public class User {//可以把value放在成员变量上或者set方法上//放在成员变量上 是通过反射的Fileld复制,破坏了封装性//放在sey方法上是通过Set方法复制,推荐使用//虽然放在成员变量上破坏了封装性但是更方便阅读,所以看个人喜欢把@Value("zhangsan") private String name;//@Autowired 和 @Qualifier 一般是配套使用,但是更建议使用@Resource(name="car")//@Autowired   //自动专配 //问题:如果匹配多个类型一致的对象,将无法选择注入哪一个对象//@Qualifier("car") //值填写bean的name@Resource(name="car") //手动注入,指定注入对象private Car car;//在注入对象的时候,一定要先把这个对象配置到到域中  也就在Car类上加个 @Component("car")public Car getCar() {return car;}public void setCar(Car car) {this.car = car;}public String getName() {return name;}public void setName(String name) {this.name = name;}@PostConstruct   //在对象被创建后调用  init-methodprivate void init() {System.out.println("初始化方法");}@PreDestroy     //在对象被销毁之前调用 destory-methodprivate void destory() {System.out.println("销毁方法");}@Overridepublic String toString() {return "User [name=" + name + ", car=" + car + "]";} }

4-2 Car实体  辅助实体

package springDemo.bean;import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component("car")
public class Car {@Value("红色")private String color;private String name;public String getColor() {return color;}public void setColor(String color) {this.color = color;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Car [color=" + color + ", name=" + name + "]";}}

5、applicationContext.xml 配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "><!-- 开启使用注解代替配置文件 --><!-- 指定扫描springDemo.bean包下的所有类中的注解,也会扫描下面所有的子包 --><context:component-scan base-package="springDemo.bean"></context:component-scan>
</beans>

6、测试

package springDemo.test;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import springDemo.bean.User;public class Demo {@Testpublic void fun1(){//1、创建容器对象ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");//2、向容器要对象User u = (User) ac.getBean("user");System.out.println(u);}
}

代码亲测有效

Spring常用注解介绍 [附带代码]相关推荐

  1. spring 常用注解 原理 逻辑 代码演示

    01 spring 常用注解 原理 逻辑 代码演示 这是自己观看视频的笔记 文章目录 01 spring 常用注解 原理 逻辑 代码演示 一.组件注册 1.1-spring注解驱动开发 1.2-组件注 ...

  2. Spring常用注解,注解 IOC ,AOP,MVC 的理解

    文章目录 大佬总结 0.Spring常用注解 0.1.SpringBootController里面的用法 0.1.1.SpringBoot 中常用注解@Controller/@RestControll ...

  3. Spring系列之Spring常用注解总结

    参看博客:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺 ...

  4. Spring常用注解总结

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1.如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml文 ...

  5. spring autowired idea都匹配上了_你清楚这几个Spring常用注解吗?

    作者:平凡希http://cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 如 ...

  6. Java 必须掌握的 12 种 Spring 常用注解

    转载自  Java 必须掌握的 12 种 Spring 常用注解 1.声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repo ...

  7. Spring常用注解 - 使用注解来构造IoC容器

    Spring常用注解 使用注解来构造IoC容器 用注解来向Spring容器注册Bean.需要在applicationContext.xml中注册<context:component-scan b ...

  8. autowired注解_Spring系列之Spring常用注解总结

    作者:平凡希来源:https://www.cnblogs.com/xiaoxi/p/5935009.html 传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做 ...

  9. sessionattribute 被spring 扫描不到_Spring 系列之 Spring 常用注解总结(肝硬化的干货)...

    传统的Spring做法是使用.xml文件来对bean进行注入或者是配置aop.事物,这么做有两个缺点: 1. 如果所有的内容都配置在.xml文件中,那么.xml文件将会十分庞大:如果按需求分开.xml ...

  10. java 知识积累_java学习知识积累-spring常用注解

    java学习知识积累-spring常用注解 类的分层注解,由于我们后台开发都是分为三层进行开发的,所以Spring框架提供了三种对于不同层的注解方式: 控制层:@Controller 服务层:@Ser ...

最新文章

  1. ubuntu vsftpd虚拟用户配置/ubuntu12.04上搭建vsftpd服务示例linux
  2. android stub.asinterface是什么意思
  3. u3d 模版测试 失败_基于Python的HTTP接口自动化测试框架实现
  4. linux双网卡私网,linux双网卡路由配置私网专线
  5. 天猫方糖 篇一:新版天猫放糖改造立体声
  6. vue.js安装与配置
  7. 位运算求两个数的平均值
  8. assert断言的概念
  9. 微信小程序源码免费下载
  10. ZigBee模块无线传输星形拓扑组网结构简介
  11. 局域网中工作组和域之间的区别
  12. linux下的IO重定向与管道相关的知识简析
  13. Mac电脑用预览功能调整图像大小?Mac调整图片大小方法
  14. string和字符数组的reverse函数
  15. 会声会影X10视频制作软件中文密钥激活版
  16. correl函数相关系数大小意义_Excel使用Correl函数返回相关系数并确定属性关系的步骤方法...
  17. Ant Design Vue 相关介绍
  18. html 不出现水平滚动条,html – 不带滚动条的水平滚动
  19. 轻松快捷的安装Testlink,终于可以轻松搞定!
  20. html倒计时10s,vue做30s倒计时,在最后10s倒数的时候有个放大的效果

热门文章

  1. 自制 APP Inventor 蓝牙接收数据接收模板 自制遥控小车App的必经之路~
  2. XPIR : Private Information Retrieval for Everyone论文阅读笔记
  3. 捋一捋字符串和字节序列的关系
  4. js-原生Js汉语拼音首字母匹配城市名
  5. 光伏发电量和用电量的概率预测研究综述(1)
  6. 春风吹又生(1年工作经验感悟)
  7. 使用AT89C51芯片实现生日快乐歌
  8. AI相关英语词汇(持续更新)
  9. GitHub使用官网指南之Hello World
  10. 深入浅出Direct3D 蔡军生 版权所有