依赖注入的方式有两种,
1.基于于XML配置文件的注入方式
2.基于注解的方式注入。
1.基于XML配置文件注入
1)set方式注入
- 注入基本数据类型
(1)需要创建Bean类
public class IoCTest {
private int intval;
private double doubleval;
private char charval;
private boolean booleanval;
private String strval;
public int getIntval() {
return intval;
}
public void setIntval(int intval) {
this.intval = intval;
}
public double getDoubleval() {
return doubleval;
}
public void setDoubleval(double doubleval) {
this.doubleval = doubleval;
}
public char getCharval() {
return charval;
}
public void setCharval(char charval) {
this.charval = charval;
}
public boolean isBooleanval() {
return booleanval;
}
public void setBooleanval(boolean booleanval) {
this.booleanval = booleanval;
}
public String getStrval() {
return strval;
}
public void setStrval(String strval) {
this.strval = strval;
}
@Override
public String toString() {
return "IoCTest{" +
"intval=" + intval +
", doubleval=" + doubleval +
", charval=" + charval +
", booleanval=" + booleanval +
", strval='" + strval + '\'' +
'}';
}
}
(2)applicationContext.xml配置文件中配置
<bean id="ioc" class="org.example.IoCTest">
<property name="intval" value="15"></property>
<property name="doubleval" value="12.5"></property>
<property name="booleanval" value="true"></property>
<property name="charval" value="男"></property>
<property name="strval" value="hello world"></property>
</bean>
- 注入复合数据类型
(1)需要创建Bean类
public class IoCTest2 {
private Date dateval;
private String[] str;
private Student[] stus;
private List<Student> list;
private Set<String> set;
private Map<String,String> map1;
private Map<String,Student> map2;
private Properties ps;
public Properties getPs() {
return ps;
}
public void setPs(Properties ps) {
this.ps = ps;
}
public Date getDateval() {
return dateval;
}
public void setDateval(Date dateval) {
this.dateval = dateval;
}
public String[] getStr() {
return str;
}
public void setStr(String[] str) {
this.str = str;
}
public Student[] getStus() {
return stus;
}
public void setStus(Student[] stus) {
this.stus = stus;
}
public List<Student> getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Set<String> getSet() {
return set;
}
public void setSet(Set<String> set) {
this.set = set;
}
public Map<String, String> getMap1() {
return map1;
}
public void setMap1(Map<String, String> map1) {
this.map1 = map1;
}
public Map<String, Student> getMap2() {
return map2;
}
public void setMap2(Map<String, Student> map2) {
this.map2 = map2;
}
}
(2)Spring核心配置文件
<bean id="ioc2" class="org.example.IoCTest2">
<!-- String数组 -->
<property name="str">
<array>
<value>盖伦</value>
<value>亚索</value>
<value>豹女</value>
<value>小鱼人</value>
<value>皇子</value>
<value>赵信</value>
</array>
</property>
<!-- 对象数组 -->
<property name="stus">
<array>
<ref bean="stu"></ref>
<ref bean="stu2"></ref>
<ref bean="stu"></ref>
</array>
</property>
<!-- 对象List -->
<property name="list">
<list>
<ref bean="stu"></ref>
<ref bean="stu2"></ref>
<ref bean="stu"></ref>
</list>
</property>
<!-- 字符串Set -->
<property name="set">
<set>
<value>你好呀</value>
<value>我是谁</value>
<value>我在那</value>
<value>我在干嘛</value>
</set>
</property>
<!-- Map<String,String> -->
<property name="map1">
<map>
<entry key="zhngsan" value="张三"></entry>
<entry key="lisi" value="李四"></entry>
<entry key="wangwu" value="王五"></entry>
</map>
</property>
<!-- Map<String,Object> -->
<property name="map2">
<map>
<entry key="key1" value-ref="stu"></entry>
<entry key="key2" value-ref="stu2"></entry>
</map>
</property>
<!-- Properties对象 -->
<property name="ps">
<props>
<prop key="username">root</prop>
<prop key="password">root</prop>
<prop key="url">jdbc:mysql://localhost:3306/test</prop>
</props>
</property>
</bean>
2)构造方法注入
(1)Spring创建Bean类
public class StudentCreate {
private Student stu;
private int id;
public StudentCreate(Student stu,int id){
this.stu = stu;
this.id = id;
}
@Override
public String toString() {
return "StudentCreate{" +
"stu=" + stu +
", id=" + id +
'}';
}
}
(2)applicationContext核心配置文件
<bean id="cstu" class="org.example.StudentCreate">
<!-- <constructor-arg index="0" ref="stu"></constructor-arg> -->
<constructor-arg name="stu" ref="stu"></constructor-arg>
<constructor-arg index="1" value="5"></constructor-arg>
</bean>
2.基于注解的注入方式
在Spring中,尽管使用XML配置文件可以实现Bean装配工作,但如果应用中Bean的数量较多,会导致XML配置文件过于臃肿,从而给维护和升级带来一定的困难。
Java 从 JDK 5.0 以后,提供了 Annotation(注解)功能,Spring 也提供了对 Annotation 技术的全面支持。Spring3 中定义了一系列的 Annotation(注解),常用的注解如下。
@Component@Qualifier
@Autowired 注解配合使用,会将默认的按 Bean 类型装配修改为按 Bean 的实例名称装配,Bean 的实例名称由 @Qualifier 注解的参数指定。
此注解描述Spring中的Bean创建对象,但它是一个广泛的概念,仅仅表示一个组件(Bean)并且可以作用在任何层次【控制层/业务层/数据访问层】
@Component:默认使用@Component创建的Bean类的名称,首字母小写
@Component(name):指定的名称
使用时只需将该注解标注在相应类上即可。
@Component--默认使用@Component创建的Bean类的名称,首字母小写
@Component(name)--指定的名称
注解Bean类
import org.springframework.stereotype.Component;
@Component
@Component("student")
public class StudentBean {
public void testStudent(){
System.out.println("StudentBean的实例方法");
}
}
applicationContext.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:component-scan base-package="com.wangxing.demo1"></context:component-scan>
</beans>
@Autowired
依赖注入,默认按照Bean的类型进行装配依赖注入
Bean的属性变量、属性的Set方法及构造函数进行标注。
被注入的类
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("demo1")
public class Demo1 {
@Autowired
private StudentTest stu;
public StudentTest getStu() {
return stu;
}
public void setStu(StudentTest stu) {
this.stu = stu;
}
public void test(){
System.out.println("完成依赖注入,按照Bean的类型进行依赖注入");
{
System.out.println("可以作用于属性变量、属性的Set方法以及构造函数");
}
stu.testStudent();
}
}
依赖类
import org.springframework.stereotype.Component;
//@Component
@Component("student")
public class StudentTest {
public void testStudent(){
System.out.println("StudentBean的实例方法");
}
public String getStudent(){
return "学生对象";
}
}
applicationContext.xmlSpring核心配置文件
<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.zhujie.learn"></context:component-scan>
</beans>
@Resource
依赖注入,先按照Bean名称装配 / 后按照Bean类型进行装配
@Resource中有两个重要的属性:name和type
如果指定name属性,则按照实例名称进行转配
如果指定type属性,则按照Bean类型进行装配
如果不指定,则按照Bean实例名称装配,如果不匹配则按照Bean类型进行装配,如果都不匹配则抛出NoSuchBeanDefinitionException异常。
注入类
import org.springframework.stereotype.Component;
@Component("student")
public class StudentBean {
public StudentBean getStu(){
return new StudentBean();
}
}
依赖类
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component("factorystu")
public class FcatoryStu {
// @Resource
// @Resource(name = "student") //根据注入类的Component名字和FcatoryStu成员名进行比较
@Resource(type = "StudentBean");//根据注入类的类型和FcatoryStu成员类型进行比较
private StudentBean stud = null;
public void test(){
System.out.println("Reource注解这个,如果设置name或者type那么就按照实例名或者实例类型进行注入,如果什么都不屑" +
"那么先根据实例名进行判断和Bean对象名进行判断,那么通过Type进行类型的判断");
}
}
@Repository/@Service/@Controller
数据层
import org.springframework.stereotype.Repository;
@Repository("stu")
public class StudentDAO {
public void testRequ(){
System.out.println("测试数据层");
}
}
逻辑层
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("servicce")
public class StudentService {
@Resource
private StudentDAO stu;
public void setStu(StudentDAO stu) {
this.stu = stu;
}
public void test(){
stu.testRequ();
System.out.println("service页面");
}
}
控制层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import javax.annotation.Resource;
@Controller("control")
public class StudentControl {
@Autowired
private StudentService ss;
public void setSs(StudentService ss) {
this.ss = ss;
}
public void test(){
System.out.println("这个控制层");
ss.test();
}
}
无奈源于不够强大