通过xml的方式进行对象的实列化或属性注入或许有一些繁琐,所以在开发中常用的方式更多是通过注解的方式实现对象实例化和属性注入的。

开始之前

1.导入相关的包(除了导入基本的包还要导入aop的包);

2. 创建spring配置文件,引入约束;

3. 开启注解扫描;

使用注解创建对象

四种注解:

  1. @Component
  2. @Controller
  3. @Service
  4. @Repository

    目前这四个名字不同的注解的功能是一样的,至于为啥名字不同应该是为spring后续版本做准备吧(目前spring使用的版本是4.x的版本)。

过程:

Spring配置文件:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  7. <!-- 开启注解扫描 ,到包里面扫描类、方法、属性上面是否有注解-->
  8. <context:component-scan base-package="com.test"></context:component-scan>
  9. </beans>

Person类对象代码:

  1. import org.springframework.context.annotation.Scope;
  2. import org.springframework.stereotype.Component;
  3. /**
  4. * 用@Component创建对象,对象名为person
  5. * 用@Scope声明value为prototype,是创建多列对象
  6. */
  7. @Component(value="person")
  8. @Scope(value="prototype")
  9. public class Person {
  10. public void add() {
  11. System.out.println("............person");
  12. }
  13. }

Test测试代码:

  1. package com.test.vo;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("beanTest1.xml");
  7. Person person = (Person) context.getBean("person");
  8. person.add();
  9. }
  10. }

注解注入属性

  1. @Autowired注解进行注入(例:经Dao注入到Service中):

Daotest:

  1. package com.test.vo;
  2. import org.springframework.stereotype.Component;
  3. @Component(value="daotest")
  4. public class DaoTest {
  5. public void printDao() {
  6. System.out.println("............DaoTest");
  7. }
  8. }

ServiceTest:

  1. package com.test.vo;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.stereotype.Service;
  4. @Service(value="servicetest")
  5. public class ServiceTest {
  6. @Autowired
  7. DaoTest dao;
  8. public void printService() {
  9. System.out.println(".........Service");
  10. dao.printDao();
  11. }
  12. }

测试类Test:

  1. package com.test.vo;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class Test {
  5. public static void main(String[] args) {
  6. ApplicationContext context = new ClassPathXmlApplicationContext("beanTest1.xml");
  7. ServiceTest servicetest = (ServiceTest) context.getBean("servicetest");
  8. servicetest.printService();
  9. }
  10. }
  1. @Resource进行注入

    则ServiceTest改为:
  1. package com.test.vo;
  2. import javax.annotation.Resource;
  3. import org.springframework.stereotype.Service;
  4. @Service(value="servicetest")
  5. public class ServiceTest {
  6. @Resource(name="daotest")
  7. DaoTest dao;
  8. public void printService() {
  9. System.out.println(".........Service");
  10. dao.printDao();
  11. }
  12. }

两个注解的异同:

  • 异:

    @Autowried是Spring提供的注解,是按类型(byType)注入的。

    @Resource是JEE提供的,是按名称(byName)注入的。
  • 同:都可以写在属性和setter方法上。

    可以参考:Spring注解@Resource和@Autowired区别对比

【初识Spring】对象(Bean)实例化及属性注入(注解方式)的更多相关文章

  1. spring中bean实例化时机以及整个运转方式

    接上一篇文章,一般在servlet获取到请求之后 在service方法中就可以完成所有的请求处理以及返回,但是我们会采用更高级的MVC框架来做.也就是说所有的MVC框架入口就是serlvet中的ser ...

  2. springMvc将对象json返回时自动忽略掉对象中的特定属性的注解方式

    1.注解使用在 类名,接口头上 @JsonIgnoreProperties(value={"comid"}) //希望动态过滤掉的属性 例 @JsonIgnorePropertie ...

  3. 【初识Spring】对象(Bean)实例化及属性注入(xml方式)

    title: [初识Spring]对象(Bean)实例化及属性注入(xml方式) date: 2018-08-29 17:35:15 tags: [Java,Web,Spring] --- #初识S ...

  4. Spring中bean的四种注入方式

    一.前言   最近在复习Spring的相关内容,这篇博客就来记录一下Spring为bean的属性注入值的四种方式.这篇博客主要讲解在xml文件中,如何为bean的属性注入值,最后也会简单提一下使用注解 ...

  5. 这一次搞懂Spring的Bean实例化原理

    文章目录 前言 正文 环境准备 两个重要的Processor 注册BeanPostProcessor对象 Bean对象的创建 createBeanInstance addSingletonFactor ...

  6. spring中bean的作用域属性singleton与prototype的区别

    1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...

  7. Spring中bean标签的属性和值:

    Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...

  8. Spring声明式事务管理(基于注解方式实现)

    ----------------------siwuxie095                                 Spring 声明式事务管理(基于注解方式实现)         以转 ...

  9. Spring总结四:IOC和DI 注解方式

    首先我们要了解注解和xml配置的区别: 作用一样,但是注解写在Bean的上方来代替我们之前在xml文件中所做的bean配置,也就是说我们使用了注解的方式,就不用再xml里面进行配置了,相对来说注解方式 ...

随机推荐

  1. curl http libcurl 功能使用

    /* * This example shows a HTTP PUT operation. PUTs a file given as a command * line argument to the ...

  2. 029.[转] SSO单点登录的通用架构实现

    单点登录的通用架构实现 pphh发布于2018年4月26日 http://www.hyhblog.cn/2018/04/26/single_sign_on_arch/ 目录 1. 什么是单点登录 2. ...

  3. Spring Boot2.1.7启动zipkin-server报错:Error creating bean with name 'armeriaServer' defined in class path

    修改项目,更新组件版本时,引入了最新版本2.12.9的zipkin-server和zipkin-autoconfigure-ui时,服务启动报错: org.springframework.beans. ...

  4. Linux—运行yum报错:No module named yum

    产生原因:yum基于python写的,根据报错信息提示,是yum的python版本对应不上目前python环境的版本导致的.也就是说 有人升级或者卸载了python. 解决方式: # 查看yum版本 ...

  5. Windows Redis 开机启动后台运行

    1. 从 Redis 的安装目录进入 cmd 2. 在 cmd 中输入, 将Redis绑定为 Windows 服务, 并设置为后台启动: redis-server --service-install ...

  6. win10查看桌面壁纸路径

    如题,win10查看自己壁纸的缓存路径,就是找到壁纸的原图:在explorer里输入 %USERPROFILE%\AppData\Roaming\Microsoft\Windows\Themes 1 ...

  7. 第15讲:嵌入式SQL语句(动态SQL)

    一.动态SQL概述 1. 静态SQL vs 动态SQL ①动态SQL是相对静态SQL而言的 ②静态SQL特点:SQL语句在程序中已经按要求写好,只需要把一些参数通过变量传递给SQL语句即可 specN ...

  8. 5. Vue - 小清单实例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. GitHub如何配置SSH Key

    https://github.com/xiangshuo1992/preload.git git@github.com:xiangshuo1992/preload.git 这两个地址展示的是同一个项目 ...

  10. Pwn-level2(x64)

    题目地址 https://dn.jarvisoj.com/challengefiles/level2_x64.04d700633c6dc26afc6a1e7e9df8c94e 已经知道了它是64位了, ...