spring ioc(xml配置bean)

IoC是Spring框架的核心内容,使用多种方式完美的实现了IoC,可以使用XML配置,也可以使用注解,新版本的Spring也可以零配置实现IoC。Spring容器在初始化时先读取配置文件,根据配置文件或元数据创建与组织对象存入容器中,程序使用时再从Ioc容器中取出需要的对象。

1.xml配置实现ioc


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sdut</groupId>
    <artifactId>sdut</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>sdut</name>
    <url>http://maven.apache.org</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.0.RELEASE</spring.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
            <version>4.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
        <dependency>
            <groupId>cglib</groupId>
            <artifactId>cglib</artifactId>
            <version>3.2.4</version>
        </dependency>
    </dependencies>
</project>

接口:

package dao;

public interface IBookDao {
    /*
     添加图书
     */
    public String addBook(String bookname);
}

实现

package dao;

public class BookDao implements IBookDao{
    public String addBook(String bookname) {
        return "添加图书"+bookname+"成功!";
    }
}

service:

package service;

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

public class BookService {
    IBookDao bookDao;
    public BookService(){
        //容器
        ApplicationContext context=new ClassPathXmlApplicationContext("IOCBeans01.xml");
        //从容器中获得id为bookdao的bean
        bookDao= (IBookDao) context.getBean("bookdao");
    }
    public void storeBook(String bookname){
        System.out.println("图书上货");
        String result=bookDao.addBook(bookname);
        System.out.println(result);
    }

}

bean.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

     <bean id="bookdao" class="dao.BookDao"></bean>
</beans>

test:

import service.BookService;
public class Test {
    @org.junit.Test
    public void testStoreBook()
    {
        BookService bookservice=new BookService();
        bookservice.storeBook("《Spring MVC权威指南 第一版》");
    }
}


2.构造方法注入

person类

package entity;

public abstract class Person {
    public String name;
}

Student:

package entity;

public class Student extends Person{
    public int height;
    public Student(String name,int height){
        this.name=name;
        this.height=height;
    }

    @Override
    public String toString() {
        return "Student{" +
                "height=" + height +
                ", name='" + name + '\'' +
                '}';
    }
}

bean.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

     <bean id="bookdao" class="dao.BookDao"></bean>
     <bean id="tom" class="entity.Student">
          <constructor-arg name="name" value="张三"></constructor-arg>
          <constructor-arg name="height" value="185"></constructor-arg>
     </bean>
</beans>

school:

package entity;

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

public class School {
    public static void main(String args[]){
        ApplicationContext context=new ClassPathXmlApplicationContext("IOCBeans01.xml");
        Person tom= (Person) context.getBean("tom");
        System.out.println(tom);
    }
}

3.属性注入

address类:

package entity;

public class Address {
    private String country;
    private String city;
    public Address(){

    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString() {
        return "Address{" +
                "country='" + country + '\'' +
                ", city='" + city + '\'' +
                '}';
    }
}

配置文件:

<bean id="zhuhai" class="entity.Address">
         <property name="country" value="中国"></property>
         <property name="city" value="珠海"></property>
    </bean>

测试:

Address address= (Address) context.getBean("zhuhai");
        System.out.println(address);

4.bean中对象的引用

Person类中加上address属性:

package entity;

public abstract class Person {
    public String name;
    public Address address;
}

Student类

package entity;

public class Student extends Person{
    public int height;
    public Student(String name,int height){
        this.name=name;
        this.height=height;
    }
    public Student(String name,int height,Address address){
        this.name=name;
        this.height=height;
        this.address=address;
    }

    @Override
    public String toString() {
        return "Student{" +
                "height=" + height +
                ", name='" + name + '\'' +
                '}'+address;
    }
}

配置文件:

<bean id="zhuhai" class="entity.Address">
          <property name="country" value="中国"></property>
          <property name="city" value="珠海"></property>
     </bean>

     <bean id="tom" class="entity.Student">
          <constructor-arg name="name" value="张三"></constructor-arg>
          <constructor-arg name="height" value="185"></constructor-arg>
          <constructor-arg name="address" ref="zhuhai"></constructor-arg>
     </bean>

ref代表的是引用这个对象,相当于传入得是对象的引用。而value是引入的这个对象名字的字符串。至于使用哪个是依据你所用的属性类型决定的。

测试:

 ApplicationContext context=new ClassPathXmlApplicationContext("IOCBeans01.xml");
        Person tom= (Person) context.getBean("tom");
        System.out.println(tom);

结果:

Student{height=185, name='张三'}Address{country='中国', city='珠海'}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值