Spring入门学习手册 2:怎么用注解来DI/IOC
目录
一、如果使用注解的话,在配置文件中应该做什么?
在beans标签后加上一个
<context:annotation-config/>
复制代码
标签来声明将要使用注解就可以了。
<?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:annotation-config/><bean name="c" class="com.learn.springDemo.Category"><property name="name" value="Eather"></property><property name="id" value="12345"></property></bean><bean name="p" class="com.learn.springDemo.Product"><property name="name" value="a product"></property><property name="id" value="1008611"></property><!--将下面这行注释掉,因为代码中将使用自动装配来将categroy实例注入product--><!--<property name="category" ref="c"></property>--></bean></beans>
复制代码
二、注解应该怎么用?
如果是使用@Autowired注解的话,可以加在两个地方前面。
属性前面 以及 setter方法前面。
如下代码:
package com.learn.springDemo;import org.springframework.beans.factory.annotation.Autowired;public class Product {private int id;private String name;//加在这个地方@Autowiredprivate Category category;public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}//加在这个地方@Autowiredpublic void setCategory(Category category) {this.category = category;}public int getId() {return this.id;}public String getName() {return this.name;}public Category getCategory() {return this.category;}
}复制代码
在这里必须要import org.springframework.beans.factory.annotation.Autowired;
运行结果:
a product's id is 1008611 and its category name is Eather
复制代码
三、注入同一个类的不同实例应该怎么办?
上一篇笔记 里提到了 xml里面可以写多个相同的bean吗 这个问题,也就是注入同一个类的不同实例应该怎么办?在使用xml配置时很简单,改一下name就行。
此时就可以使用@Resource注解
先改一下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 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:annotation-config/><bean name="c" class="com.learn.springDemo.Category"><property name="name" value="Eather"></property><property name="id" value="12345"></property></bean><bean name="cc" class="com.learn.springDemo.Category"><property name="name" value="David"></property><property name="id" value="10086"></property></bean><bean name="p" class="com.learn.springDemo.Product"><property name="name" value="a product"></property><property name="id" value="1008611"></property><!--将下面这行注释掉--><!--<property name="category" ref="c"></property>--></bean></beans>
复制代码
注解应该加在属性的前面
package com.learn.springDemo;import org.springframework.beans.factory.annotation.Autowired;import javax.annotation.Resource;public class Product {private int id;private String name;@Resource(name = "c")private Category category;@Resource(name = "cc")private Category category1;public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}public void setCategory(Category category) {this.category = category;}public void setCategory1(Category category1) {this.category1 = category1;}public int getId() {return this.id;}public String getName() {return this.name;}public Category getCategory() {return this.category;}public Category getCategory1() {return category1;}
}
复制代码
测试代码:
package com.learn.springTest;import com.learn.springDemo.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String args[]) {ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});Product p = (Product) ac.getBean("p");System.out.println(p.getName() + "'s id is " + p.getId() + " and its category name is " + p.getCategory().getName() + " and " + p.getCategory1().getName());}}
复制代码
运行结果:
a product's id is 1008611 and its category name is Eather and David
复制代码
四、xml配置文件里可不可以 bean 也不写?
上述例子都是对 注入对象行为 的注解,对于bean对象本身的使用可不可以也用注解的方式进行,而不写到配置文件中呢?
答案当然是可以,修改一下配置文件,去掉所有beans和bean标签,加上
<context:component-scan base-package="com.learn.springDemo"/>
复制代码
base-package 用来指定要扫描的包。
<?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.learn.springDemo" /></beans>
复制代码
类定义代码是:
package com.learn.springDemo;import org.springframework.stereotype.Component;@Component("c")
public class Category {private int id;private String name = "我是一个Category";public int getId() {return this.id;}public String getName() {return this.name;}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}
}package com.learn.springDemo;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;@Component("p")
public class Product {private int id;private String name = "我是一个product";@Autowiredprivate Category category;public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}public void setCategory(Category category) {this.category = category;}public int getId() {return this.id;}public String getName() {return this.name;}public Category getCategory() {return this.category;}
}
复制代码
测试代码是:
package com.learn.springTest;import com.learn.springDemo.Product;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String args[]) {ApplicationContext ac = new ClassPathXmlApplicationContext(new String[]{"applicationContext.xml"});Product p = (Product) ac.getBean("p");System.out.println("product 名字是 " + p.getName() + " category 名字是 " + p.getCategory().getName());}}
复制代码
运行结果是:
product 名字是 我是一个product category 名字是 我是一个Category
复制代码
至于前面的第三个问题应该怎么解决,暂时我也不知道,可能这个问题没有太大的意义吧!
Spring入门学习手册 2:怎么用注解来DI/IOC相关推荐
- Spring入门学习手册 1:最简单的反转控制
Spring入门学习手册 1:最简单的反转控制 一.什么是Javabean 看到的一个比较专业的解释是: JavaBean定义了一组规则 JavaBean就是遵循此规则的平常的Java对象 JavaB ...
- Spring入门学习手册 6:Spring MVC基础中的基础
目录 完整代码在这 一.获取请求参数 Spring获取请求参数非常简单,只要用到 @RequestParam 注解就可以了 如果不指定请求method的话,无论是get还是post参数都可以轻易获取到 ...
- 开源推荐:Asp.Net Core入门学习手册!
前言 推荐一个入门级的.NET Core开源项目,非常适合新手入门学习.NET Core. 开源地址: https://github.com/windsting/little-aspnetcore-b ...
- spring入门学习导引
从以下几个方面来简单的说一下个人对这两者的差别: ①springmvc使用更加的简便,更加的灵活,与spring的结合的更好. ②struts2相对来说比springmvc要重(哈哈 重也有重的好处 ...
- [Spring入门学习笔记][静态资源]
遗留问题 在上一节课的作业中,我们一定遇到了一点问题--虽然将页面内容正确的返回给了浏览器,但是浏览器显示的样式却是不正确的,这是因为在HTML的\标签中我们这样引入了CSS资源: <link ...
- Spring 入门学习二之IOC
今天来学习Spring ioc . 一.spring jar 包导入 在 spring 官网下载开发包 spring-framework-4.2.4.RELEASE,然后导入需要的 jar 包到项目 ...
- GeoMesa-空间数据存储引擎入门学习手册
GeoMesa-空间数据存储引擎 geomesa简介.架构体系.数据存储.spark等 第一部分:GeoMesa简介 GeoMesa是一款开源的基于分布式计算系统的⾯面向海海量量时空数据查询与分析的⼯ ...
- spring框架学习笔记3:使用注解代替配置文件
1.导入context约束:spring-context-4.2.xsd 2.design模式打开xml配置文件,右键edit namespaces,点击add添加 完成后应该是这样: 配置文件中这样 ...
- spring mvc学习(19):cookievalue注解(显示cookie的值,默认必须有值)
目录结构 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi= ...
最新文章
- Windows下Core_Audio_APIs的使用简介
- c++ 0.你好,世界
- 前端学习(2989):vue+element今日头条管理--项目结束
- AFNetworking 3.0迁移指南
- imx6 配置串口波特率_stm32异步串口(uart)通讯基本操作
- Maven学习总结(45)——Maven仓库、将本地Jar包安装到仓库、打可执行的Jar总结
- hibernate - Transaction not successfully started
- matlab 脚本文件 函数,Matlab 脚本文件script和函数文件function的区别
- 怎样在电脑上面简单的记账,了解账户收支
- 【iOS篇】在iPhone上安装描述文件
- android 解决ScrollView中的子布局不能够填充整个ScrollView
- Java教程-Java 程序员们值得一看的好书推荐
- EAX、ESP、EBP等寄存器的作用
- jQuery ajax 请求 和 Submit 提交 form 表单
- mac上截图的快捷键以及一些快捷键使用
- 华为v8原生态android,安卓原生系统的手机有哪些_2019安卓原生系统手机推荐_飞翔教程...
- flutter web 微信公众号开发记录
- java分页查询,技术总监都拍手叫好
- 大淘宝用户平台技术团队单元测试建设
- WCF Data transfer buffered VS streamed