Spring篇--02 Spring IOC、注解
发布日期:2021-06-29 15:41:55 浏览次数:3 分类:技术文章

本文共 6290 字,大约阅读时间需要 20 分钟。

Spring篇--02 Spring IOC

一、IOC(Inversion of Controll)控制反转

1.IOC是什么?

    对象之间的依赖关系由容器来建立

2.DI(Dependency Injection 依赖注入)

  容器通过调用对象的set方法或者构造器来建立依赖关系

注意:IOC是目标 DI是手段

3.set方式注入

step1:提供相应的set方法

step2:使用<property>元素

package ioc;public class A {	private IB b;		public void setB(IB b) {		System.out.println("setB()方法执行了");		this.b = b;	}		public A() {		System.out.println("A()");	}	public void execute() {		b.f1();		System.out.println("execute()");	}}
package ioc;public class B implements IB {	public B() {		System.out.println("B()");	}		public void f1() {		System.out.println("B's f1()");	}}

 

4.构造器方法注入

step1:添加相应的构造器

step2:配置constructor-arg元素

package ioc2;public class B {	public B() {		System.out.println("B()");	}	public void f1() {		System.out.println("B's f1()");	}}
package ioc2;public class A {	private B b;	public A() {		System.out.println("A()");	}	public A(B b) {		System.out.println("A(B)");		this.b = b;	}		public void execute() {		b.f1();		System.out.println("execute");	}}

5.自动装配

自动装配,指的是spring容器依据某种规则,自动建立对象之间的依赖关系。

注意:a.默认情况下,容器不会自动装配

b.可以通过指定autowire属性来告诉容器进行自动装配(容器仍然需要通过set方法或者构造器来完成依赖关系的建立)

package ioc2;public class Waiter {	public Waiter() {		System.out.println("waiter()");	}	}
package ioc2;public class Restaurant {	private Waiter wt;	public Restaurant() {		System.out.println("Restaurant()");	}	public void setWt(Waiter wt) {		System.out.println("set()");		this.wt = wt;	}	@Override	public String toString() {		return "Restaurant [wt=" + wt + "]";	}				}

6.注入基本类型的值

使用value属性即可

7.注入集合类型的值

List  Set  Map Properties

北京
上海
深圳
广州
读书
看报
晒太阳
小花
5210
package value;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;public class ValueBean {	private String name;	private int age;	private List
city; private Set
interest; private Map
score; private Properties db; public ValueBean() { System.out.println("ValueBean()"); } public void setAge(int age) { this.age = age; } public void setName(String name) { this.name = name; } public void setCity(List
city) { this.city = city; } public void setDb(Properties db) { this.db = db; } public void setScore(Map
score) { this.score = score; } public void setInterest(Set
interest) { this.interest = interest; } @Override public String toString() { return "ValueBean [name=" + name + ", age=" + age + ", city=" + city + ", interest=" + interest + ", score=" + score + ", db=" + db + "]"; } }

8.引入方式注入集合类型的值

上海
杭州
北京
书法
绘画
拳击
小草
0220

9.读取properties的值

10.使用spring表达式

可以使用spring表达式读取其他bean的属性,它的语法类似于el表达式

二、使用注解简化配置

 

1.组件扫描

spring容器在启动之后,会扫描指定的包及其子包下面的所有的类,如果该类前面有特定的注解(比如@Component),则spring容器会将这个类纳入容器进行管理(相当于在配置文件当中,配置了一个bean元素)。

2.如何进行组件扫描

step1:在类前添加特定的注解

注:除了@Component注解,还有@Service,@Repository和@Controller,作用是等价的,只不过有语义上的差异。

package annotation;import javax.annotation.PostConstruct;import javax.annotation.PreDestroy;import org.springframework.context.annotation.Lazy;import org.springframework.context.annotation.Scope;import org.springframework.stereotype.Component;@Component("sb1")       //组件bean@Scope("singleton")    //指定作用域@Lazy(true)            //延迟加载//单例public class SomeBean {	@PostConstruct	public void init() {		System.out.println("init()");	}	@PreDestroy	public void destory() {		System.out.println("destory");	}		public SomeBean() {		System.out.println("SomeBean()");	}}

step2:要在配置文件中,添加组件扫描的配置

3.依赖注入相关的注解

@Autowired和@Qualifier

a.该注解支持set方式注入和构造器方式的注入

b.当采用set方式注入时,可以将@Autowired添加到set方法前面,如果不使用@Qualifier,则容器会采用byType的方式来注入,有可能出错,所以建议使用@Quarlifer注解,明确指定要注入的bean的id。

注:也可以将这两个注解直接添加到属性前

c.当采用构造器注入时,可以 将该注解添加到对应的构造器前面即可。

package annotation;import org.springframework.stereotype.Component;@Component("wt")public class Waiter {	public Waiter() {		System.out.println("Waiter()");	}}
package annotation;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.stereotype.Component;@Component("rt")public class Restaurant {	@Autowired	@Qualifier("wt")		private Waiter wt;	public void setWt(Waiter wt) {		System.out.println("setWt()");		this.wt = wt;	}	public Restaurant() {		System.out.println("Restaurant");	}	@Override	public String toString() {		return "Restaurant [wt=" + wt + "]";	}	}
@Test	//测试@Autowired(set方式注入)	public void test1() {		ApplicationContext ac=new ClassPathXmlApplicationContext("annotation.xml");		Restaurant rt = ac.getBean("rt",Restaurant.class);		System.out.println(rt);	}

@Resource

a.只支持set方式的注入

b.可以将该注解添加到属性前 ,使用name属性指定要注入的bean的id(如果不指定,会按照byType的方式注入)

注:也可以将该注解添加到属性前

package annotation;import javax.annotation.Resource;import org.springframework.stereotype.Component;@Component("bar")public class Bar {	private Waiter wt;	public Bar() {		System.out.println("Bar()");	}	@Resource(name="wt")	public void setWt(Waiter wt) {		System.out.println("setWt()");		this.wt = wt;	}	@Override	public String toString() {		return "Bar [wt=" + wt + "]";	}}

 

4.@Value注解

a.可以使用该注解来注入基本类型的值

b.也可以使用该注解来使用spring表达式

c.该注解可以添加到属性前,或者添加到对应的set方法前

package annotation;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Component("mg")public class Manager {	@Value("#{config.pagesize}")	private String pageSize;	@Value("小草")	private String name;	@Override	public String toString() {		return "Manager [pageSize=" + pageSize + ", name=" + name + "]";	}	public Manager() {		System.out.println("Manager()");	}	}

 

 

 

转载地址:https://codingchaozhang.blog.csdn.net/article/details/80005819 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Spring篇--03 Spring MVC之建立第一个spring项目
下一篇:Spring篇--01 Spring简介、Spring容器

发表评论

最新留言

第一次来,支持一个
[***.219.124.196]2024年04月05日 08时04分52秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章