这篇文章主要介绍了Spring项目XML文件使用常见介绍,主要包括项目pom文件,项目初始IOC容器及项目需要自动装配的代码详解,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下
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>com.yang</groupId>
<artifactId>spring-study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>spring-01-ioc1</module>
<module>spring-02-hellpspring</module>
<module>spring-04-di</module>
<module>spring-06-autowired</module>
<module>spring-07-annotation</module>
</modules>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.4.RELEASE</version>
</dependency>
</dependencies>
</project>
···
2 项目初始IOC容器
```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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- more bean definitions go here -->
</beans>
···
3 项目需要自动装配
```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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<bean id="dog" class="com.yang.pojo.Dog"></bean>
<bean id="cat" class="com.yang.pojo.Cat"></bean>
<bean id = "people" class="com.yang.pojo.People">
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
<property name="name" value="张3"/>
</bean>
</beans>
增加的点:
1 xmlns:context="http://www.springframework.org/schema/context"等等头文件
2 context:annotation-config/
4 项目需要注解
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 指定要扫描的包,包下面的注解才能够生效-->
<context:component-scan base-package="com.yang.pojo"/>
<!-- 注解驱动-->
<context:annotation-config/>
</beans>
增加的点:
<context:component-scan base-package=“com.yang.pojo”/>
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
到此这篇关于Spring项目XML文件使用常见介绍的文章就介绍到这了,更多相关Spring项目XML文件使用内容请搜索编程学习网以前的文章希望大家以后多多支持编程学习网!
织梦狗教程
本文标题为:Spring项目XML文件使用小结


基础教程推荐
猜你喜欢
- Project Reactor源码解析publishOn使用示例 2023-04-12
- JVM分析之类加载机制详解 2023-04-06
- 一文了解Java 线程池的正确使用姿势 2023-06-17
- Java去掉小数点后面无效0的方案与建议 2023-02-18
- SpringBoot配置文件中密码属性加密的实现 2023-03-11
- Java使用EasyExcel进行单元格合并的问题详解 2023-01-18
- Java File类的概述及常用方法使用详解 2023-05-18
- 工厂方法在Spring框架中的运用 2023-06-23
- 全局记录Feign的请求和响应日志方式 2023-01-09
- 用java实现扫雷游戏 2022-12-06