1.XML配置版
1.pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--如果Spring要实现AOP,使用AOP织入,需要导入一个依赖包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
2.beans.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">
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<bean id="..." class="...">
<!-- collaborators and configuration for this bean go here -->
</bean>
<!-- more bean definitions go here -->
</beans>
举例版:
<?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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!--
具p-命名空间的XML快捷方式加上:xmlns:p="http://www.springframework.org/schema/p"
具c-namespace的XML快捷方式加上:xmlns:c="http://www.springframework.org/schema/c",C命名一定要有有参构造器
-->
<!--使用Spring来创建对象,在Spring这些都称为Bean
类型 变量名 = new 类型();
Address address= new Address();
id = 变量名
class = new 的对象;
property 相当于给对象中的属性设置一个值!
ref : 引用Spring容器中创建好的对象
value : 具体的值,基本数据类型!
-->
<bean id="address" class="com.loey.pojo.Address" scope="prototype">
<property name="address" value="hanguo"></property>
</bean>
<!-- p命名-->
<bean id="stu" class="com.loey.pojo.Student" p:name="pcy" p:address-ref="address" p:books="1,2,3"/>
<!-- c命名-->
<bean id="stu1" class="com.loey.pojo.Student" c:name="pcyc" c:address-ref="address"/>
<bean id="student" class="com.loey.pojo.Student">
<property name="name" value="pcy"></property>
<property name="address" ref="address"></property>
<property name="books">
<array>
<value>1</value>
<value>2</value>
<value>3</value>
</array>
</property>
<property name="hobbys">
<list>
<value>a</value>
<value>b</value>
<value>c</value>
<value>d</value>
</list>
</property>
<property name="card">
<map>
<entry key="1" value="11"/>
<entry key="2" value="22"/>
<entry key="3" value="33"/>
</map>
</property>
<property name="games">
<set>
<value>aa</value>
<value>bb</value>
</set>
</property>
<property name="wife">
<null/>
</property>
<property name="info">
<props>
<prop key="url">com.mysql.jdbc.Driver</prop>
<prop key="username">url</prop>
<prop key="password">1127</prop>
</props>
</property>
</bean>
</beans>
3.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="beans.xml"/>
</beans>
4.测试获取上下文对象
//获取spring的上下文对象
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Student student = (Student) context.getBean("student");
Student stu = context.getBean("stu", Student.class);
// Student stu1 = context.getBean("stu1", Student.class);
// System.out.println(student.toString());
System.out.println(stu.toString());
// System.out.println(stu1.toString());
2.注解版
1.pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--如果Spring要实现AOP,使用AOP织入,需要导入一个依赖包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
2.beans.xml
利用注解的方式注入属性加上约束
xmlns:context=“http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
<?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
http://www.springframework.org/schema/context/spring-context.xsd">
<!--指定要扫描的包,这个包下的注解就会生效-->
<context:component-scan base-package="com.loey.pojo"/>
<!--开启注解的支持-->
<context:annotation-config/>
</beans>
3.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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="beans.xml"/>
</beans
详细看Spring使用注解开发