Spring的import标签的使用

35 篇文章 0 订阅
24 篇文章 1 订阅

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>text</groupId>
    <artifactId>Spring01</artifactId>
    <version>1.0.0</version>
    <packaging>jar</packaging>

    <dependencies>

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>5.0.0.RELEASE</version>
        </dependency>
    </dependencies>

    <build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-resources-plugin</artifactId>
            <version>2.6</version>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/java</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
    </build>


</project>

applicationContext.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">

    <!--导入其他的spring配置文件
        resource:从classes路径开始寻找文件
        classpath:表示指定从classes路径开始寻找配置文件
    -->

    <import resource="classpath:text/spring/_01_hello/applicationContext_hello.xml"/>
    <import resource="classpath:text/spring/_02_configuration/applicationContext_configuration.xml"/>


</beans>

_01_hello:
App.java:

package text.spring._01_hello;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * Created by Administrator on 2019/9/2.
 */
public class App {
    @Test
    public void HelloWorld(){
//        读取spring的配置文件
        Resource resource=new ClassPathResource("applicationContext.xml");
//        启动容器
        BeanFactory factory=new XmlBeanFactory(resource);
//        1、通过对象的名字获取,需要类型转化
//        HelloWorld helloWorld = (HelloWorld) factory.getBean("helloWorld");
//        2、通过类型获取
//        HelloWorld helloWorld = factory.getBean(HelloWorld.class);
//        3、通过名字和类型共同获取(推荐)
        HelloWorld helloWorld = factory.getBean("helloWorld2", HelloWorld.class);
        System.out.println(helloWorld);

    }
}

HelloWorld.java:

package text.spring._01_hello;

/**
 * Created by Administrator on 2019/9/2.
 */
public class HelloWorld {
    private String name;
    private Integer age;

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

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

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

applicationContext_hello.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">

    <!--创建谁的对象
        class:创建的对象的类的全限定名
        id:给对象的一个名字,在spring容器中的该对象的唯一标识,通常为简单类名,首字母小写
    -->
    <bean class="text.spring._01_hello.HelloWorld" id="helloWorld1">
        <property name="name" value="rose"></property>
    </bean>

    <bean class="text.spring._01_hello.HelloWorld" id="helloWorld2">
        <property name="name" value="jack"></property>
    </bean>

</beans>

_02_configuration:
App.java:

package text.spring._02_configuration;

import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

/**
 * Created by Administrator on 2019/9/2.
 */
public class App {
    @Test
    public void test(){
        //        读取spring的配置文件
        Resource resource=new ClassPathResource("applicationContext.xml");
        //        启动容器
        BeanFactory factory=new XmlBeanFactory(resource);
        User user = (User) factory.getBean("user");
        System.out.println(user);
    }


}

User.java:

package text.spring._02_configuration;

/**
 * Created by Administrator on 2019/9/2.
 */
public class User {
    private String name;

}

applicationContext_configuration.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="user" class="text.spring._02_configuration.User">

    </bean>
</beans>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值