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

Spring 依赖注入之从不会到稍微会一点儿

时间:2017-06-04 11:44:01      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:path   frame   private   lap   xmla   setname   ons   struct   getname   

 

<?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="user" class="io.zekai.entity.User" >
        <!--<constructor-arg type="int" value="1994"></constructor-arg>-->
        <!--<constructor-arg type="String" value="大仙"></constructor-arg>-->
        <property name="id" value="10000"/>
        <property name="name" value="狂尊"/>
    </bean>
</beans>

 

 

package io.zekai.entity;

/**
 * Created by zekai on 2017/6/3.
 */
public class User {
    private int id;
    private String name;

    public User() {
    }

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

 

package io.zekai.entity;

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

/**
 * Created by zekai on 2017/6/3.
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("./io/zekai/entity/application-context.xml");
        //通过构造器 根据bean的唯一id
        User user=context.getBean(User.class);
        System.out.println(user.getName());
        System.out.println(user.getId());
    }
}

 

<?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="user" class="io.zekai.entity.User" >
        <!--<constructor-arg type="int" value="1994"></constructor-arg>-->
        <!--<constructor-arg type="String" value="大仙"></constructor-arg>-->
        <property name="id" value="10000"/>
        <property name="name" value="狂尊"/>
    </bean>
    <bean id="fatherUser" class="io.zekai.entity.UserFather">
        <property name="fatherName" value="laodies"/>
        <property name="user" ref="user"/><!--ref参考的是xml中的-->
    </bean>
</beans>

 

 

package io.zekai.entity;

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

/**
 * Created by zekai on 2017/6/3.
 */
public class Main {
    public static void main(String[] args) {
        ApplicationContext context=new ClassPathXmlApplicationContext("io/zekai/entity/application-context.xml");
        UserFather user=context.getBean(UserFather.class);
        System.out.println(user.getFatherName());
    }
}

 

Spring 依赖注入之从不会到稍微会一点儿

标签:path   frame   private   lap   xmla   setname   ons   struct   getname   

原文地址:http://www.cnblogs.com/tangzekai/p/6939752.html

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