jsp页面可正常显示,但是提交表单信息报错404
IOC反转控制:将我们自己创建对象的工作交给Spring容器帮我们完成
DI依赖注入:将值通过配置的方式为变量初始化赋值(注入)
两季都学完啦,如果课程可以单独买就太好啦
解决web项目中只需要一个spring容器
利用ServletContextListener 通过配置监听器来打到我们的需求,在web项目创建时创建spring容器,销毁时候关闭spring容器
在web.xml中配置 监听器,
<!-- 配置监听器,在web项目启动的时候让spring启动 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener.class</listener-class>
</listener>
<!-- 读取spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
在UserLoginServlet中:
//ServletContextListener 可以通过配置监听器来打到我们的需求,在web项目创建时创建spring容器,销毁时候关闭spring容器
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
us = (UserService)wac.getBean("userService");
在applicationContext.xml中配置:
<!-- 配置dataSource -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssm_spring?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- 配置dao -->
<bean name="userDao" class="com.lingzhihao.dao.UserDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置service -->
<bean name="userService" class="com.lingzhihao.service.UserServiceImpl">
<property name="ud" ref="userDao"/>
</bean>
构造函数注入:
配置文件中:
<!-- 构造方法注入 -->
<bean name="user1" class="com.lingzhihao.bean.User">
<!-- name 调用构造方法的参数名称 value 是注入值类型 ref注入引用类型 -->
<!-- type 指定参数的类型 -->
<!-- index 指定参数的索引位置 -->
<constructor-arg name="u_username" value="11" type="java.lang.Integer" index="0"/>
<constructor-arg name="petType" ref="dog"/>
</bean>
User类中:
public User(String u_username, Pet petType) {
System.out.println("方法1 String, Pet");
this.u_username = u_username;
this.petType = petType;
}
public User(Integer u_username, Pet petType) {
System.out.println("方法2 Integer, Pet");
this.u_username = u_username.toString();
this.petType = petType;
}
public User( Pet petType,Integer u_username) {
System.out.println("方法3 Pet ,Integer");
this.u_username = u_username.toString();
this.petType = petType;
}
Spring整合Junit单元测试
1.导包:test包
2.使用@RunWith 注解创建 spring 容器
3.使用@ContextConfiguration 读取 spring 配置文件
@RunWith(SpringJUnit4ClassRunner.class)//使用junit进行测试,帮我们创建容器
@ContextConfiguration("classpath:applicationContext_Injection.xml")//读取配置文件
public class Test_JUnit {
@Resource(name="dog")
private Pet p;
@Test
public void Test2() {
System.out.println(p);
}
}
主配置文件的分包配置
如果都卸载一个配置文件里,就会很混乱
所以分包写配置文件,然后集中在一个主配置文件中
<!-- 导入其他 Spring 的配置文件 -->
<import resource="/applicationContext_Injection.xml"/>
注解配置-属性注入
@Value("1")
private Integer u_id;//使用暴力反射区注入
@Value("老张") //推荐在set方法上注入
public void setU_username(String u_username) {
this.u_username = u_username;
}
//自动装配
@Autowired
推荐:
//手动装配
@Resource(name="dog")
Spring注解配置:
导包和约束: aop包 + context约束
后面使用spring code editer打开 xml文件,将context添加
将对象注册到容器内
在xml中添加:
<!-- 注解开发 -->
<!-- 开启组件扫描 扫描包下的所有注解-->
<context:component-scan base-package="com.lingzhihao.bean"></context:component-scan>
注解:
@Component("user")
@Controller() 对应web层
@Service() 对应service
@Repository() 对应dao层
用注解配置Scope属性
@Scope(scopeName="prototype")
注解配置init-method 与 destroy-method
//在构造方法后调用
@PostConstruct()
public void userInit() {
System.out.println("userInit");
}
//在销毁方法钱调用
@PreDestroy()
public void userDestroy() {
System.out.println("destroy");
}
注解配置属性注入,值类型与引用类型
<!-- 复杂类型注入 -->
<bean name="collection" class="com.lingzhihao.bean.MyCollection">
<!-- array -->
<property name="array" value="123"/>
<!-- <property name="array">
<array>
<value>123</value>
<value>abc</value>
<ref bean="dog"/>
</array>
</property> -->
<!-- list -->
<property name="list">
<list>
<value>456</value>
<value>def</value>
<ref bean="user1"/>
</list>
</property>
<!-- set -->
<property name="set">
<set>
<value>789</value>
<value>ghi</value>
<ref bean="user1"/>
</set>
</property>
<!-- map -->
<property name="map">
<map>
<entry key="username" value="李四"/>
<entry key="password" value="123"/>
<entry key-ref="user1" value-ref="dog"></entry>
</map>
</property>
<!-- properties -->
<property name="properties">
<props>
<prop key="name">老李</prop>
<prop key="age">38</prop>
</props>
</property>
</bean>
set注入方式:
<!-- 将user对象交给spring管理,并注入值类型 -->
<bean name="user" class="com.lingzhihao.bean.User">
<property name="u_id" value="1"></property>
<property name="u_username" value="老王"></property>
<property name="u_password" value="123"></property>
<!-- 引用类型注入 将value 替换为 ref-->
<property name="petType" ref="dog"></property>
</bean>
<bean name="dog" class="com.lingzhihao.bean.Pet">
<property name="petType" value="哈士奇"></property>
<property name="color" value="灰色"></property>
</bean>
初始化方法:Init-method
销毁方法:destroy_method
如果使用的是scope=“prototype”,则不会执行销毁方法
xml配置-bean标签-scope属性
prototype 多列的 在获取的时候会创建多个对象
request 在web环境下,如果scope属性为request name这个对象被创建出来,他的生命周期会与request请求一致
session 生命周期与session一致
singleton默认值 单列的 只创建一个对象
name 可以通过这个name来医用容器获取对象
id与name作用基本相同,但不推荐使用,因为不支持特殊字符,不推荐使用
layz-init="true" 延迟加载
ioc反转:创建对象这份工作由我们自己执行反转给spring帮我们执行;
ioc控制:就是由spring帮我们负责创建销毁对象,掌控对象的生命周期等,我们在需要使用对象的时候跟spring申请即可
ioc是一种编程思想,也是一种新的设计模式,他想要DI(依赖注入)的支持
让spring来管理对象,
在applicationContext.xml中,创建
<bean name="user" class="com.lingzhihao.bean.User"></bean>
然后在Test类中,进行测试,
可以通过容器里的对象名来获取:
User user = (User) ac.getBean("user");
也可以通过class,字节码对象来获取:
User user = ac.getBean(User.class);
spring的 命名规则
在src下创建xml,命名:applicationContext.xml需要导入头文件,
然后点击添加,添加一个 xsi
在继续添加导入的本地的主包:
spring-beans.xsd
Class