java web之路 spring 基于@Autowried注解的依赖注入
2018-03-02 16:52
281 查看
spring 2.5以后可以使用注解的方式添加注入,在xml中这种方式默认是关闭的,需要手动打开,即在xml中添加
不使用注解xml配置为:<bean id="textEditor" class="tutorialspoint.TextEditor" scope="prototype">
<constructor-arg ref="spellChecker"></constructor-arg>
</bean>
<bean id="spellChecker" class="tutorialspoint.SpellChecker" scope="prototype"></bean>注解在属性上的应用package autowired;
import org.springframework.beans.factory.annotation.Autowired;
public class TextEditor {
@Autowired
private SpellChecker spellChecker;
public SpellChecker getSp() {
return spellChecker;
}
public void setSp(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
public void spellChecker(){
spellChecker.checkSpelling();
}
}依赖的类package autowired;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor.");
}
public void checkSpelling(){
System.out.println("Inside checkSpelling." );
}
}main方法package autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] arg) {
// TODO Auto-generated constructor stub
ApplicationContext con = new ClassPathXmlApplicationContext("autowired.xml");
TextEditor ted = (TextEditor)con.getBean("textEditor");
ted.spellChecker();
}
}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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 开启注解方式 -->
<context:annotation-config />
<bean id="textEditor" class="autowired.TextEditor"></bean><!-- 使用注解方式后,不需再写property属性 -->
<bean id="spellChecker" class="autowired.SpellChecker"></bean>
</beans>
<context:annotation-config />@Autowried注解可以用在属性、构造函数、set方法中。在需要注入的类中添加@Autowried注解,在使用的时候,spring会自动加载类实例。
不使用注解xml配置为:<bean id="textEditor" class="tutorialspoint.TextEditor" scope="prototype">
<constructor-arg ref="spellChecker"></constructor-arg>
</bean>
<bean id="spellChecker" class="tutorialspoint.SpellChecker" scope="prototype"></bean>注解在属性上的应用package autowired;
import org.springframework.beans.factory.annotation.Autowired;
public class TextEditor {
@Autowired
private SpellChecker spellChecker;
public SpellChecker getSp() {
return spellChecker;
}
public void setSp(SpellChecker spellChecker) {
this.spellChecker = spellChecker;
}
public void spellChecker(){
spellChecker.checkSpelling();
}
}依赖的类package autowired;
public class SpellChecker {
public SpellChecker(){
System.out.println("Inside SpellChecker constructor.");
}
public void checkSpelling(){
System.out.println("Inside checkSpelling." );
}
}main方法package autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] arg) {
// TODO Auto-generated constructor stub
ApplicationContext con = new ClassPathXmlApplicationContext("autowired.xml");
TextEditor ted = (TextEditor)con.getBean("textEditor");
ted.spellChecker();
}
}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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 开启注解方式 -->
<context:annotation-config />
<bean id="textEditor" class="autowired.TextEditor"></bean><!-- 使用注解方式后,不需再写property属性 -->
<bean id="spellChecker" class="autowired.SpellChecker"></bean>
</beans>
相关文章推荐
- Spring依赖注入的简单示例(基于注解配置)
- Spring——基于注解方式的依赖注入
- java web之路 spring 基于构造函数的依赖注入
- Spring学习3—控制反转(IOC)基于Annotation(注解)的依赖注入实现
- spring注解基于Annotation的依赖注入配置笔记
- 【JavaWeb-23】spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- 框架 day36 Spring3 入门,DI依赖注入,装配bean基于xml/注解, 整合Junit4,配置约束自动提示
- spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- Spring学习3—控制反转(IOC)基于Annotation(注解)的依赖注入实现
- Spring源码解析-基于注解依赖注入
- spring2.5 基于注解依赖注入学习--01
- Spring依赖注入:注解注入总结
- 详解 Spring 3.0 基于 Annotation 的依赖注入实现
- MyBatis+Spring在注解@Autowried后通过反射的方式调用方法获取注入的Service或DAO对象为空
- Spring依赖注入——java项目中使用spring注解方式进行注入
- Spring Boot2.x-05Spring Boot基础-使用注解完成依赖注入
- Spring依赖注入:注解注入总结
- 【第十二章】零配置 之 12.2 注解实现Bean依赖注入 ——跟我学spring3
- 详解 Spring 3.0 基于 Annotation 的依赖注入实现
- Spring依赖注入:注解注入总结