关于Spring框架的入门可以参考以下的连接https://blog.csdn.net/weixin_43883917/article/details/112644783?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-0&spm=1001.2101.3001.4242
创建spring框架项目
导入springframe依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.8</version>
</dependency>
之后可设置context文件
如何通过tomcat启动spring容器?
配置web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--监听context的启动-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
context.xml文件详解:
该文件相当于bean类的对spring容器的注册声明,之后可交给spring创建对象。这些创建有一定的方法:
1.spring容器作用域:
bean标签的scope属性指定作用域,默认为single,单例模式:只创建一次对象无论调用context的getbean方法多少次
设定为原型,prototype:每次的getbean都创建对象。
可以参考以下连接
https://keafmd.blog.csdn.net/article/details/112687252
2.spring延迟加载:
取决于作用域:》》》如果是单例启动就创建对象-无延迟,ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
就会创建。
》》》如果为原型不会马上创建,当调用getbean时创建对象-延迟加载(优化
3.DI 属性参数注入:
1.set注入:
只有当你写了get set方法时xml中property才能设置对应的属性的值
创建cat类
package com;
import java.util.List;
public class Cat {
private String name;
private int age;
private List interest;
public Cat(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List getInterest() {
return interest;
}
public void setInterest(List interest) {
this.interest = interest;
}
@Override
public String toString() {
return super.toString();
}
}
contextCat.xml
注意list属性是如何编写的
<?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="cat" class="com.Cat" >
<property name="name" value="加菲猫"></property>
<property name="age" value="3"></property>
<property name="interest" >
<list>
<value>"catch mouse"</value>
<value>"catch fish"</value>
<value>"sleep"</value>
</list>
</property>
</bean>
</beans>
test类中编写
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("contextCat.xml");
Cat cat = applicationContext.getBean("cat",Cat.class);
System.out.printf("name:"+cat.getName());
System.out.printf("age:"+cat.getAge());
System.out.printf("interest"+cat.getInterest());
输出如下
以上只是简单的单个类
下面来看类作为属性的情况:
编写teacher类:
package com;
public class Teacher {
String name;
String age;
Student student;
public void talk(){
System.out.printf("别睡了"+student.getName());
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
xml
特别注意 ref属性
<bean id="stu" class="com.Student">
<property name="name" value="sleep"></property>
</bean>
<bean id="teacher" class="com.Teacher">
<property name="student" ref="stu"></property>
</bean>
test:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("contextCat.xml");
applicationContext.getBean("teacher",Teacher.class).talk();
结果
2.构造器注入
编写dog,只有get方法
package com;
public class Dog {
//只添加get方法
private String name;
private int age;
public Dog(String name,int age){
this.age = age;
this.name = name;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
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="dog" class="com.Dog">
<constructor-arg name="name" value="lucky"></constructor-arg>
<constructor-arg name="age" value="3"></constructor-arg>
</bean>
</beans>
3注解声明bean(主要方法)
bean
package com;
import org.springframework.stereotype.Component;
import java.text.Bidi;
@Component("bird")//注解声明id
public class Bird {
public Bird(){
System.out.println("实例化一个bird"+this);
}
}
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 http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 开启注解扫描 -->
<context:component-scan base-package="com"/>
</beans>
test:
ApplicationContext applicationContext1 = new ClassPathXmlApplicationContext("contextBird.xml");
applicationContext1.getBean("bird",Bird.class);//引用声明的id
下面是对xml文件的注入附加理解的案例:
3 注入表达式
3.1 问题
如何通过表达式的方式,给bean注入值。
3.2 方案
创建一个资源文件const.properties,用来封装系统常量,代码如下:
1. #max rows in one page
2. PAGE_SIZE=10
新建一个bean,通过表达式的方式引用MessageBean等bean的数据,注入给这个新建的bean。
<!-- 声明Properties集合,读取const.properties参数 此时获得一个const 的bean-->
2. <util:properties id="const" location="classpath:const.properties"/>
3. <!-- 注入表达式 -->
4. <bean id="demo" class="com.AAA.bean.DemoBean">
5. <property name="name" value="#{msg.name}"/>
6. <property name="lang" value="#{msg.langs[0]}"/>
7. <property name="score" value="#{msg.score.JSD1412001}"/>
8. <property name="pageSize" value="#{const.PAGE_SIZE}"/>
4.自动装配
使用xml的autowire属性,在启动时自动注入。
使用autowire属性可以实现自动注入,可以取值:byName和byType。
简单示例代码:
<bean id="registerAction" class="com.lantsky.action.RegisterAction" scope="prototype">
<property name="userManageService">
<ref bean="userManageService"/>
</property>
</bean>
可以写成:
<bean id="registerAction" class="com.lantsky.action.RegisterAction" scope="prototype" autowire="byName"/>