【初识Spring】对象(Bean)实例化及属性注入(注解方式)
通过xml的方式进行对象的实列化或属性注入或许有一些繁琐,所以在开发中常用的方式更多是通过注解的方式实现对象实例化和属性注入的。
开始之前
1.导入相关的包(除了导入基本的包还要导入aop的包);
2. 创建spring配置文件,引入约束;
3. 开启注解扫描;
使用注解创建对象
四种注解:
- @Component
- @Controller
- @Service
- @Repository
目前这四个名字不同的注解的功能是一样的,至于为啥名字不同应该是为spring后续版本做准备吧(目前spring使用的版本是4.x的版本)。
过程:
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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解扫描 ,到包里面扫描类、方法、属性上面是否有注解-->
<context:component-scan base-package="com.test"></context:component-scan>
</beans>
Person类对象代码:
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
/**
* 用@Component创建对象,对象名为person
* 用@Scope声明value为prototype,是创建多列对象
*/
@Component(value="person")
@Scope(value="prototype")
public class Person {
public void add() {
System.out.println("............person");
}
}
Test测试代码:
package com.test.vo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beanTest1.xml");
Person person = (Person) context.getBean("person");
person.add();
}
}
注解注入属性
- @Autowired注解进行注入(例:经Dao注入到Service中):
Daotest:
package com.test.vo;
import org.springframework.stereotype.Component;
@Component(value="daotest")
public class DaoTest {
public void printDao() {
System.out.println("............DaoTest");
}
}
ServiceTest:
package com.test.vo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service(value="servicetest")
public class ServiceTest {
@Autowired
DaoTest dao;
public void printService() {
System.out.println(".........Service");
dao.printDao();
}
}
测试类Test:
package com.test.vo;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beanTest1.xml");
ServiceTest servicetest = (ServiceTest) context.getBean("servicetest");
servicetest.printService();
}
}
- @Resource进行注入
则ServiceTest改为:
package com.test.vo;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
@Service(value="servicetest")
public class ServiceTest {
@Resource(name="daotest")
DaoTest dao;
public void printService() {
System.out.println(".........Service");
dao.printDao();
}
}
两个注解的异同:
- 异:
@Autowried是Spring提供的注解,是按类型(byType)注入的。
@Resource是JEE提供的,是按名称(byName)注入的。 - 同:都可以写在属性和setter方法上。
可以参考:Spring注解@Resource和@Autowired区别对比
【初识Spring】对象(Bean)实例化及属性注入(注解方式)的更多相关文章
- spring中bean实例化时机以及整个运转方式
接上一篇文章,一般在servlet获取到请求之后 在service方法中就可以完成所有的请求处理以及返回,但是我们会采用更高级的MVC框架来做.也就是说所有的MVC框架入口就是serlvet中的ser ...
- springMvc将对象json返回时自动忽略掉对象中的特定属性的注解方式
1.注解使用在 类名,接口头上 @JsonIgnoreProperties(value={"comid"}) //希望动态过滤掉的属性 例 @JsonIgnorePropertie ...
- 【初识Spring】对象(Bean)实例化及属性注入(xml方式)
title: [初识Spring]对象(Bean)实例化及属性注入(xml方式) date: 2018-08-29 17:35:15 tags: [Java,Web,Spring] --- #初识S ...
- Spring中bean的四种注入方式
一.前言 最近在复习Spring的相关内容,这篇博客就来记录一下Spring为bean的属性注入值的四种方式.这篇博客主要讲解在xml文件中,如何为bean的属性注入值,最后也会简单提一下使用注解 ...
- 这一次搞懂Spring的Bean实例化原理
文章目录 前言 正文 环境准备 两个重要的Processor 注册BeanPostProcessor对象 Bean对象的创建 createBeanInstance addSingletonFactor ...
- spring中bean的作用域属性singleton与prototype的区别
1.singleton 当一个bean的作用域设置为singleton, 那么Spring IOC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会 ...
- Spring中bean标签的属性和值:
Spring中bean标签的属性和值: <bean name="user" class="com.pojo.User" init-method=" ...
- Spring声明式事务管理(基于注解方式实现)
----------------------siwuxie095 Spring 声明式事务管理(基于注解方式实现) 以转 ...
- Spring总结四:IOC和DI 注解方式
首先我们要了解注解和xml配置的区别: 作用一样,但是注解写在Bean的上方来代替我们之前在xml文件中所做的bean配置,也就是说我们使用了注解的方式,就不用再xml里面进行配置了,相对来说注解方式 ...
随机推荐
- 剑指offer 13:数值的整数次方
题目描述 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. 保证base和exponent不同时为0 问题分析 计算一个浮点数的整数次 ...
- Android,百度,云知声tts总结
最近在做Android语音播报功能(TTS),现总结如下:(ps:demo代码地址:https://github.com/giserlong/TTS_DEMO) 一.Android原生接口 用Andr ...
- [20190515]热备份模式与rman冲突.txt
[20190515]热备份模式与rman冲突.txt --//别人的系统做dg时打开热备份模式,忘记关闭,做rman备份时报错.做一个记录.--//实际上也怪自己,实施时没有讲清楚.通过例子说明: 1 ...
- Webshell篇
常用方法简介: 一.0day拿webshell 参考工具:织梦漏洞利用小工具 二.通过注入漏洞拿Webshell 前提条件:具有足够权限,对写入木马的文件夹要有写入权限,知道网站绝对路径. 对于mss ...
- 01 python的安装
下载3.7.0版本 然后点击 close 关闭 1-==>搜索输入“cmd”输入 “python -V”并回车. 出现版本说明安装成功.2==>>>> 是提示符3=== ...
- Html学习之七(CSS选择器的使用--基础选择器优先级问题)
二.基础选择器的综合使用 优先级顺序:id选择器>class选择器>元素选择器.也就是说,如果这三种选择器同时为某一个元素设定样式,那么冲突的部分按优先级的顺序依次决定. <!DOC ...
- luoguP1791 [国家集训队]人员雇佣
题意 考虑先将所有价值加上,之后用最小割求最小代价. 考虑每个点对\((i,j)\),我们这样建边: 1.源点向每个点i连\(\sum\limits E_{i,j}\)容量的边. 2.每个点向汇点连雇 ...
- C++面向对象程序设计学习笔记(6)
多态性 编译时的多态性与运行时的多态性 在面向对象方法中,所谓多态性就是不同对象收到相同信息时,产生不同的行为.在c++程序设计中,即"一个接口,多种方法" 在C++中,多态性的实 ...
- cicros安装
1.下载与安装依赖包 wget https://github.com/libgd/libgd/releases/download/gd-2.2.5/libgd-2.2.5.tar.gz tar zxv ...
- WordPress更改“固定链接”后 ,页面出现404的解决方法
一.Web服务器对应的是Nginx 解决方案:修改linux服务器下Nginx的配置文件,目录为:/usr/local/nginx/conf/nginx.conf, 也可以直接使用命令nginx -t ...