我是靠谱客的博主 威武龙猫,最近开发中收集的这篇文章主要介绍Spring的import标签的使用,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

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>

最后

以上就是威武龙猫为你收集整理的Spring的import标签的使用的全部内容,希望文章能够帮你解决Spring的import标签的使用所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(46)

评论列表共有 0 条评论

立即
投稿
返回
顶部