spring之重温回顾

  • Post author:
  • Post category:其他


bean的获取

属性注入

1.基本属性注入

①构造方法注入

    <bean class="org.sidney.ioc.model.User" id="user">
        <constructor-arg name="id" value="1"/>
        <constructor-arg name="username" value="sidney"/>
        <constructor-arg name="address" value="www.dizhou.com"/>
    </bean>

②set方法注入

    <bean class="org.sidney.ioc.model.User" id="user2">
        <property name="id" value="2"/>
        <property name="username" value="sidney"/>
        <property name="address" value="你好 Spring"/>
    </bean>

③p名称空间注入


    <bean class="org.sidney.ioc.model.User" id="user3" p:username="sidney" p:address="hello spring" p:id="3">
    </bean>

2.工厂方法注入

①静态工厂注入

②实例工厂注入

3.复杂属性注入

①对象注入

每个user拥有一只cat

@Data
public class User {
    private String username;

    private String address;

    private Integer id;

    private Cat cat;

    public User(String username, String address, Integer id) {
        this.username = username;
        this.address = address;
        this.id = id;
    }

    private User(){
        System.out.println("--------------init---------------");
    }

}
@Data
public class Cat {
    private String name;
    private Integer age;
}

   <bean class="org.sidney.ioc.model.User" id="user4">
        <property name="id" value="4"/>
        <property name="username" value="sidney"/>
        <property name="address" value="你好 Spring"/>
        <property name="cat" ref="cat"/>
    </bean>

    <bean class="org.sidney.ioc.model.Cat" id="cat">
        <property name="age" value="3"/>
        <property name="name"  value="Tom"/>
    </bean>

②数组注入

@Data
public class User {
    private String username;

    private String address;

    private Integer id;

    private Cat cat;

    private Cat[] cats;

    private List<String> favorites;


    public User(String username, String address, Integer id) {
        this.username = username;
        this.address = address;
        this.id = id;
    }

    private User(){
        System.out.println("--------------init---------------");
    }

}

③map注入

④Properties注入

@Data
public class User {
    private String username;

    private String address;

    private Integer id;

    private Cat cat;

    private Cat[] cats;

    private List<String> favorites;

    private Map<String,String> getDetails;

    private Properties info;




    public User(String username, String address, Integer id) {
        this.username = username;
        this.address = address;
        this.id = id;
    }

    private User(){
        System.out.println("--------------init---------------");
    }

}
<bean class="org.sidney.ioc.model.User" id="user4">
        <property name="id" value="4"/>
        <property name="username" value="sidney"/>
        <property name="address" value="你好 Spring"/>
        <property name="cat" ref="cat"/>
        <property name="cats">
            <!--数组中可以ref引用或自定义bean-->
            <array>
                <!--引用-->
                <ref bean="cat"/>
                <!--自定义-->
                <bean class="org.sidney.ioc.model.Cat" id="cat2">
                    <property name="age" value="5"/>
                    <property name="name" value="Jerry"/>
                </bean>
            </array>
        </property>

        <property name="favorites">
            <list>
                <value >读书</value>
                <value >电影</value>
            </list>
        </property>

        <!--map注入-->
        <property name="getDetails" >
            <map>
                <entry key="gender" value="男"/>
                <entry key="age"    value="99"/>
            </map>
        </property>

        <!--Properties注入-->
        <property name="info">
            <props>
                <prop key="phone">13578965412</prop>
            </props>
        </property>
    </bean>

    <bean class="org.sidney.ioc.model.Cat" id="cat">
        <property name="age" value="3"/>
        <property name="name"  value="Tom"/>
    </bean>

注解配置

public class SayHello {
    public String sayHello(String name){
        return "hello "+name;
    }
}
@Configuration //表示是一个Java配置类,作用类似于applicationContext.xml
public class JavaConfig {
   @Bean("sh")
    SayHello sayHello(){
       return new SayHello();
   }

}
public class JavaConfigTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class);
        SayHello sayHello = context.getBean("sh", SayHello.class);
        System.out.println(sayHello.sayHello("sidney"));

    }
}

2.自动扫描

bean的名字 默认的是类首字母小写,但也可以在注解中取名



版权声明:本文为weixin_35969228原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。