spring MVC

Spring总结:实现三大功能
1、创建对象及初始化
2、实现MVC层次零耦合
3、对方法进行切面管理
springMVC m

m 为模型

v 为视图  

c 为控制器

applicationContext.xml
bean:只要把一个java类放到spring容器中就是一个bean
<beans>
<bean id="" class=""></bean>
<!--把工厂类导入
factory-method:工厂方法
-->
<!-- scope:prototype -->
<!-- lazy-init:true[default][false] -->
<!-- init-method:init -->
<!-- destory-method:destory -->
<bean id="hellwordFactory" class="..." factory-method="getInstance">
<!-- 别名-->
<alias name="hellword" alias="狗蛋"></alias>
<alias name="hellword" alias="王二麻子"></alias>
<!-- 属性
property:用来描述类的属性和赋值
name 属性的值来源于类的set方法
value:赋值(基本类型的包装类型与String类型)
ref:指向spring内部的对象(引用类型赋值)
-->
<property name="pid" value="1"></property>
<property name="pname" value="狗蛋"></property>
<property name="student" ref="..Student"></property>
或者<property name="student">
<ref bean="student"/>
</property>
<property name="student">
<list>
<value>list1</value>
<ref bean=""/>
</property>
<property name="student">
<set>
<value>set1</value>
<ref bean=""/>
</set>
</property>
<property name="student">
<set>
<value>set1</value>
<ref bean=""/>
</set>
</property>
<property name="student">
<map>
<entry key="m1">
<value>map1</value>
</entry
<entry key="m2">
<value>map2</value>
</entry
</map>
</property>
<property name="properties">
<props>
<prop key="pid">
pid
</prop>
</props>
</property>

<!-- 用来描述构造函数的参数
index构造函数的参数位置,从0开始
type构造函数的类型
ref 赋值一个引用
value赋值基本类型
-->
<constructor-arg index="0" value="fds"/>
<constructor-arg index="1" value="fdf"/>
</bean>
<bean id="student" class="..Student">
说明:
1、person对象对于student是由依赖性的,所以在spring容器内部做的事情:
1、创建person对象
2、创建student对象
3、调用set方法赋值
*如果person的bean中没有配置student对象:
1、创建person对象
2、调用set方法赋值
3、创建student对象
2、一般情况下,把list,map,set等信息封装到spring容器的配置文件中,这些集合一般都存放固定的信息
<!--  导入其他配置文件-->
<import resource=""></import>
</beans>
springIOC(控制反转)容器
1、对象的创建(面试:spring怎么产生对象的)
采用默认的构造函数
<bean id="hellWord" class="..HellWord"></bean>
采用静态工厂方法
HelloWorldFactory返回了HelloWorld的实例
<bean id="helloWorld2" class="....HelloWorldFactory" factory-method="getInstance"></bean>
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld2");
spring容器就是调用了工厂方法而已,而产生对象的逻辑是由程序员完成的
采用对象工厂
说明:只要在spring的配置文件中配置bean,spring容器就会为该bean创建对象
 如果把该bean中的默认的构造函数去掉,则会报错
2、对象的scope(单例还是多例)
在spring容器中默认的产生对象的scope是单例的
验证:getInstance两次,打印类名一样
注意:如果把一个数据声明到该类的属性中,那么这个数据将成为全局共享的数据
掌握:把一个bean在spring容器中变成多例
<bean id="" class="" scope="prototype"></bean>
3、初始化bean的实例
*将来spring容器和web容器整合,如果当web容器启动的时候,spring容器就会为bean创建对象
*如果spring容器中的bean写错,在web容器启动的时候就会报错
*如果是在context.getBean时,才要为bean创建对象,这个时候,spring配置文件中的错误就会隐藏掉了
默认情况:在spring容器启动的时候创建对象
在配置文件的bean中加入lazy-init=”true“,那么bean就会延迟创建对象, 在context.getBean时才要创建
<bean id="" class=""  lazy-init="true"></bean>
场景:如果一个bean已经spring容器中了,并且该bean中需要大量的数据的时候,延迟加载稍微好点
4、初始化和销毁
初始化方法:在bean配置中加该属性 init-method=”init“,但是该方法是由spring调用的
销毁:在bean配置中加该属性 destory-method=”destory“,该方法是由spring调用的
当spring容器关闭的时候或者销毁的时候,执行该方法,在该方法中可以做一些资源的关闭的工作
ClassPathXmlApplicationContext context2 = (ClassPathXmlApplicationContext)context;
context2.close();
context2.destory();
说明:spring容器管理bean的生命周期:对象的创建、初始化、销毁工作
注意:
*如果一个bean的scope为prototype,即便spring容器销毁,但是该bean却没有销毁
*如果一个bean的scope为prototype,lazy-init的属性将会失效,都是在context.getBean时创建对象
 需求:action是在请求的时候创建对象,并不是在web容器启动的时候创建对象的
5、别名
<bean id="hellwordFactory" class="..." factory-method="getInstance">
<!-- 别名-->
<alias name="hellword" alias="狗蛋"></alias>
<alias name="hellword" alias="王二麻子"></alias>
</bean>
context.getBean("狗蛋")
6、import的用法:
<!--  导入其他配置文件-->
<import resource=""></import>
7、<!-- spring继承-->
<bean id="person" class="...Person" abstract="true"> //不能被实例化

<bean id="student" class="...Student" parent="person" >

 

DI:依赖注入(给属性赋值)

构造函数描述:
在配置文件中可以采用constructor-arg的方式描述构造函数,并且,一个bean只能描述一个构造函数
<constructor-arg index="0" value="fds"/>
<constructor-arg index="1" value="fdf"/>
说明:spring会根据你传的参数个数和参数的类型匹配相应的构造函数,完成初始化
利用注解完成DI
注解:
1、注解解析器
2、注解不能单独存在
@Resource注解过程
1、public class Person{
@Resource
private Student student;
}
2、配置文件
命名空间
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
3、bean的配置
<bean id="person" class=""></bean>
<bean id="student" class=""></bean>
4、注解解析器
<context:annotation=config></context:annotation=config>
原理:
1、当启动spring容器的时候因为有两个bean,所有这两个bean被实例化了
2、spring容器会去解析<context:annotation=config></context:annotation=config>
3、会在纳入spring管理的bean的范围内查找,看那些bean的方法或者属性上添加了@Resource注解
4、会去检查@Resource的那么属性是否为”“
如果为”“,则会按照属性的名称和spring中id进行匹配,如果匹配成功则赋值,如果匹配不成功,则按照类型进行匹配,匹配成功则赋值,不成功则报错
如果不为”“,则会按照那么属性的值和spring中的id进行匹配,如果匹配不成功则直接报错
其他依赖注入:
@Autowired按照类型进行自动匹配:按照类型进行匹配是一件很危险的事
@Autowired
@Qualifier(value="student")一起作用就相当于@Resource
类扫描:
1、既包含了类扫描的,也包含了依赖注入的注解解析器
<context:component-scan base-package="包名"></context:component-scan>
2、如果想把一个雷放入到spring容器中,则类上加一个@Component(Value=”person“)注解
例如:
*@Component(value="personn")
public class Personn{}
==
<bean id="person" class="..Person">
*@Component
public class Persdsn{}
==
<bean id="persdsn" class="..Persdsn">
3、在属性上加@Resource(name="student")注解
原理:
1、当启动spring容器的时候,会去解析配置文件
找到<
2、就会去base-package指定的包及子包中查找类,看哪些类加了@Component-scan注解
3、按照@Component-scan的匹配规则,去创建对象
4、去对象中查找对象的@Resource(name="student")注解,按照@Resource(name="student")注解的匹配规则给属性赋值
xml与注解的依赖注入
1、xml写法比较发展,但效率比较高
2、注解写法比较简单,但效率比家底
3、在xml文件中应用注解越多,效率越低
4、spring基于注解的配置为引用类型

MVC架构降耦合
<bean id="userDao" class="cn.itheima02.spring.mvc.UserDaoImpl"></bean>
<bean id="userService" class="cn.itheima02.spring.mvc.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userAction" class="cn.itheima02.spring.mvc.UserActionImpl">
<property name="userService" ref="userService"></property>
</bean>
注解的方法是给MVC架构降耦合
@Repository(value="")、@Service(value="")、@Controller(value="")分别对应了持久化层、服务层和表现层
它们都是将对应的类注入到spring容器中
@Resource(name="")将该引用类型注入到对应类中
@Repository(value="")中value相当于是定义注入类
@Resource(name="")中name相当于是引用类
<context:component-scan base-package="cn.itheima02.spring.mvc.component_scan"></context:component-scan>
-------------------------
@Repository(value="userDao")
public class UserDaoImpl implements UserDao{
-------------------------
@Service(value="userService")
public class UserServiceImpl implements UserService{
@Resource(name="userDao")
private UserDao userDao;
-------------------------
@Controller(value="userAction")
public class UserActionImpl implements UserAction{
@Resource(name="userService")
private UserService userService;

原文地址:https://www.cnblogs.com/zbf1214/p/5265102.html