一、创建一个maven项目
略
二、在pom.xml文件中配置spring
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
</dependencies>
配置完成后记得导包!
三、配置spring
在resources文件夹里新建一个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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
</beans>
四、使用bean标签在spring中创建对象
在(三)中的xml文件中配置
<!-- bean标签的作用是用来在spring容器中创建对象的,里面有两个重要的属性
id的作用是指定该对象的对象名
class的作用是指定对应的类
-->
<bean id="hello" class="com.haina.spring.HelloService">
<!-- 给name属性赋值 -->
<property name="name" value="张三"/>
<property name="age" value="20"/>
</bean>
但是到目前为止,如果我们直接运行spring还是不能给我们创建对象,我们还需要有一步操作
就是在测试类里写:
//读取配置文件,创建spring容器对象
String conf = "applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(conf);
getBean方法使用bean标签配置的id来从容器中获取对象
//getBean方法使用bean标签配置的id来从容器中获取对象
HelloService h1 = (HelloService) context.getBean("hello");
h1.sayHello();
注意:强制类型转换的原因是,getBean返回的是Object类型的对象!
为什么?
因为getBean方法是spring中的方法,而spring在定义getBean方法的时候是不知道我们需要什么类型的对象的,所以返回的是Object类型的对象。
当然,使用类型来获取对象的时候,是不用强转的
//使用类型来获取对象,使用类型来获取对象的时候,不需要强转
//使用类型来获取对象的时候,要注意如果该类型的对象有多个,是无法使用的
HelloService h3 = context.getBean("hello2",HelloService.class);
h3.sayHello();
五、代码
resources文件夹下的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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 配置组件扫描 -->
<context:component-scan base-package="com.haina.spring"/>
<!-- bean标签的作用是用来在spring容器中创建对象的,里面有两个重要的属性
id的作用是指定该对象的对象名
class的作用是指定对应的类
-->
<bean id="hello" class="com.haina.spring.HelloService">
<!-- 给name属性赋值 -->
<property name="name" value="张三"/>
<property name="age" value="20"/>
</bean>
<!-- 构造器注入有三种形式,参数名注入,下标注入,类型注入。 可以混用,但不建议 -->
<bean id="hello2" class="com.haina.spring.HelloService">
<!-- <constructor-arg name="name" value="aaa"/>-->
<!-- <constructor-arg name="age" value="21"/>-->
<!-- <constructor-arg index="0" value="aaa"/>-->
<!-- <constructor-arg index="1" value="21"/>-->
<constructor-arg type="java.lang.String" value="aaa"/>
<constructor-arg type="int" value="21"/>
</bean>
<!-- 对于这些第三方的类,我们只能使用配置文件来创建对象,而无法使用注解 -->
<bean id="now" class="java.util.Date"/>
<!-- 当我们的字符串中包含特殊字符,value属性中无法写的话,需要把原本的
value属性,变成value标签,然后使用<![CDATA[a<b]]> 来添加内容 -->
<bean id="h3" class="com.haina.spring.HelloService">
<property name="name" >
<value><![CDATA[a<b]]]></value>
</property>
</bean>
<bean id="payService" class="com.haina.spring.PayService">
</bean>
<!-- autowire属性可以设置自动装配,给该类对象中的属性自动赋值
byType是根据类型自动赋值,要求同一个类型不能有多个bean,否则不能使用byType
byName是根据名称自动赋值,要求类中的属性名必须与bean的id相同,否则找不到
-->
<bean id="order" class="com.haina.spring.OrderService" autowire="byName">
<!-- 当我们要给引用类型的属性复制时,不能使用value属性
而是要是用ref属性,值就是对应bean的id -->
<!-- <property name="payService" ref="pay"/>-->
</bean>
<aop:aspectj-autoproxy/>
<!-- 定义一个切面类对象-->
<bean id="as1" class="com.haina.spring.aop.AspectTest1"/>
<aop:config>
<aop:pointcut id="p" expression="execution(public * com.haina.spring.aop.CalculatorImpl.*(..))"/>
<aop:aspect ref="as1">
<!-- 通过p对应的表达式,找到对应的类中的方法,然后再那个方法执行之前,来执行beforeMethod方法 -->
<aop:before method="beforeMethod" pointcut-ref="p"/>
<aop:after method="afterMethod" pointcut-ref="p"/>
<aop:after-returning method="afterReturn" pointcut-ref="p" returning="a"/>
<aop:after-throwing method="afterException" pointcut-ref="p"/>
<aop:around method="around" pointcut-ref="p"/>
</aop:aspect>
</aop:config>
<bean id="cal" class="com.haina.spring.aop.CalculatorImpl"/>
</beans>
main/java/com.haina.spring/HelloService.java:
package com.haina.spring;
public class HelloService {
private String name;
private int age;
public void sayHello(){
System.out.println("hello spring" + name + age);
}
public HelloService(){
}
public HelloService(String name,int age){
this.name = name;
this.age = age;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
}
main/java/com.haina.spring/SpringTest.java:
package com.haina.spring;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//spring测试类
/*
* spring容器初始化的流程:
* 1、加载配置文件
* 2、Spring会读取配置文件中的bean标签
* 3、通过bean标签的class属性的值,找到该bean对应的类
* 4、使用反射技术创建出该类的对象,存储到spring容器中
* 5、spring容器我们可以理解为是一个HashMap
* 6、对象存储时,key是该bean的id,value是对象
*
*/
public class SpringTest {
public static void main(String[] args) {
//传统的创建对象,调用方法的形式
//当我们在项目中只需要该类的一个对象,不需要重复性的创建时
//可以使用spring容器来帮我们管理对象
HelloService hs = new HelloService();
hs.sayHello();
//读取配置文件,创建spring容器对象
String conf = "applicationContext.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(conf);
//getBean方法使用bean标签配置的id来从容器中获取对象
HelloService h1 = (HelloService) context.getBean("hello");
h1.sayHello();
HelloService h2 = (HelloService) context.getBean("hello");
System.out.println(h1 == h2);
//使用类型来获取对象,使用类型来获取对象的时候,不需要强转
//使用类型来获取对象的时候,要注意如果该类型的对象有多个,是无法使用的
HelloService h3 = context.getBean("hello2",HelloService.class);
h3.sayHello();
}
}
六、总结
Spring容器初始化流程
1、加载配置文件
2、Spring会读取配置文件中的bean标签
3、通过bean标签的class属性的值,找到该bean对应的类
4、使用反射技术创建出该类的对象,存储到spring容器中
5、spring容器我们可以理解为是一个HashMap
6、对象存储时,key是该bean的id,value是对象