码迷,mamicode.com
首页 > 编程语言 > 详细

20160121--Spring

时间:2016-01-22 00:02:07      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
package com.hanqi;

public class HelloWorld {

    
    public HelloWorld()
    {
        
    }
    
    public HelloWorld(String name)
    {
        this.name = name;
    }
    
    private User user;
    
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        
        System.out.println("设置name = "  + name);
        
        this.name = name;
    }
    
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public void sayHello()
    {
        System.out.println("Hello " + name + "   " + user);
    }


}
HelloWorld.java
技术分享
package com.hanqi;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    //调用
    public static void main(String[] args) {
        
        
/*        
        //初始化
        HelloWorld hw = new HelloWorld();
        
        hw.setName("Java");
        
        hw.sayHello();
*/
        //构建容器
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //从容器中获取实例  控制反转IOC
        HelloWorld hw = (HelloWorld)ac.getBean("helloWorld");
        
        //调用实例的方法
        hw.sayHello();
        
    }

}
Main.java
技术分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="helloWorld" class="com.hanqi.HelloWorld">
<!-- 依赖注入DI -->
    <property name="name" value="Spring"></property>
    <property name="user" ref="user1"></property>

</bean>

<bean id="user" class="com.hanqi.User">
    <property name="name" value="小强"></property>
</bean>

<bean id="user1" class="com.hanqi.User"><!-- User中的 name、age、sex 一一对应 -->
    <constructor-arg value="小明" type="String"></constructor-arg>        <!-- 用 index="顺序号" 进行匹配 -->
    <constructor-arg value="12" type="int"></constructor-arg>        <!-- type="数据类型" 根据数据类型来进行匹配 -->
    <constructor-arg value="男" type="String"></constructor-arg>
    <!-- <constructor-arg value="小明" index="0"></constructor-arg>         用 index="顺序号" 进行匹配  -->
</bean>


</beans>
applicationContext.xml
技术分享
package com.hanqi;

public class User {

    public User()
    {
        
    }
    
    
    @Override
    public String toString() {
        return "User [name=" + name + ", age=" + age + ", sex=" + sex + "]";
    }

    
    public User(String name, int age, String sex) {
        super();
        this.name = name;
        this.age = age;
        this.sex = sex;
    }

    private String name;
    private int age;
    private String sex;

    
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
User.java

技术分享

20160121--Spring

标签:

原文地址:http://www.cnblogs.com/name-hanlin/p/5149663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!