spring IOC 注解@Autowired

自动装配:按照类型来找

会在xml中找类型一样的,

比如 setMessage(SetName setName)上面有@Autowired,会到xml中找<bean id="setname" class="com.yibai.SetName">

package com.yibai;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
public class HelloWorld {
    SetName setName;
    @Autowired
       public void setMessage(SetName setName){
        this.setName=setName;
        System.out.println("setmessage");
         setName.getSetName() ;
       }
}
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="helloWorld" class="com.yibai.HelloWorld">
   bean>
    <bean id="setname" class="com.yibai.SetName">
    bean>
beans>

 

转载于:https://www.cnblogs.com/paisen/p/4381831.html

你可能感兴趣的:(java)