【JAVAWEB】spring

Spring是一个开源容器框架,Spring的核心是控制反转(IOC)和面向切面(AOP)。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式)轻量级开源框架

  • IoC:控制反转。 
    举例来说,在之前的操作中,比方说有一个类,我们想要调用类里面的方法(不是静态方法),就要创建类的对象,使用对象调用方法实现。对于Spring来说,Spring创建对象的过程,不是在代码里面实现的,而是交给Spring来进行配置实现的。
  • AOP:面向切面编程。 

    三种对象创建方式:

    Spring里边是通过配置文件来创建对象的,即其有IOC控制反转机制,每一个bean的实例化就是在创建一个对象。bean实例化有三种实现方式:

    (1)使用类的无参构造函数来创建(重点)

    比如说有一个User类:

 
public class User {
	
	private String username;
	
	public User() {
		
	}
	
}

配置文件

<bean id="user" class="cn.ywq.User" ></bean> 

(2)使用静态工厂创建:

创建静态的方法,返回类对象

package cn.ywq.bean;
 
public class Bean2Factory {
 
	//静态的方法,返回Bean2对象
	public static Bean2 getBean2() {
		return new Bean2();
	}
}

<bean id="bean2" class="cn.ywq.bean.Bean2Factory" factory-method="getBean2"></bean>

(3)使用实例工厂创建

创建不是静态的方法,返回类对象 n

package cn.ywq.bean;
 
public class Bean3Factory {
 
	//普通的方法,返回Bean3对象
	public Bean3 getBean3() {
		return new Bean3();
	}
}
<bean id="bean3Factory" class="cn.ywq.bean.Bean3Factory"></bean>
<bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>

对象属性注入:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
	<!-- spring管理的对象 -->
	<!-- set属性注入 -->
	<bean name="User" class="lx.bean.User">
		<!-- 值类型注入 -->
		<property name="name" value="tom"></property>
		<property name="age" value="18"></property>
		<!-- 引用类型注入 -->
		<property name="Car" ref="Car"></property>
	</bean>
	<bean name="Car" class="lx.bean.Car">
		<property name="name" value="兰博基尼"></property>
		<property name="color" value="黄色"></property>
	</bean>
	<!-- p名称空间注入 -->
	<bean name="User2" class="lx.bean.User" p:name="jack" p:age="20"
		p:car-ref="Car"></bean>
	<!-- spel注入 -->
	<bean name="User3" class="lx.bean.User">
		<property name="name" value="#{User.name}"></property>
		<property name="age" value="#{User2.age}"></property>
		<property name="Car" ref="Car"></property>
	</bean>
	<!-- 复杂类型注入 -->
	<bean name="CollectionBean" class="lx.c_injection.CollectionBean">
		<!-- <property name="arr" value="tom"></property> -->
		<property name="arr">
			<array>
				<value>tom</value>
				<value>jack</value>
				<ref bean="User3" />
			</array>
		</property>
		<!-- <property name="list" value="tom"></property> -->
		<property name="list">
			<list>
				<value>tom</value>
				<value>jack</value>
				<ref bean="User3" />
			</list>
		</property>
		<property name="map">
			<map>
				<entry key="aa" value="tom"></entry>
				<entry key="bb" value="jack"></entry>
			</map>
		</property>
		<property name="pro">
			<props>
				<prop key="aa">tom</prop>
				<prop key="bb">jack</prop>
			</props>
		</property>
	</bean>
</beans>

AOP实现原理(动态代理:被代理对象必须实现接口、cglib代理:可以对任何类生成代理,代理的原理是对目标对象进行继承)

aop体现:配置通知

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd ">
	<!-- 配置目标对象 -->
	<bean name="userservice" class="lx.service.impl.UserServiceImpl">
	</bean>
	<!-- 配置通知对象 -->
	<bean name="myadvice" class="lx.aspect.MyAdvice"></bean>
	<!-- 配置将通知织入目标对象 -->
	<aop:config>
	<!-- 配置切入点 -->
	<!-- public void lx.service.impl.UserServiceImpl.add() -->
	<!-- void lx.service.impl.UserServiceImpl.add() -->
	<!-- * lx.service.impl.UserServiceImpl.add() -->
	<!-- * lx.service.impl.UserServiceImpl.*() -->
	<!-- * lx.service.impl.*ServiceImpl.*(..) -->
		<aop:pointcut expression="execution(* lx.service.impl.*ServiceImpl.*(..))" id="pc" />
		<aop:aspect ref="myadvice">
		<aop:before method="before" pointcut-ref="pc"/>
		<aop:after-returning method="afterReturning" pointcut-ref="pc"/>
		<aop:around method="around" pointcut-ref="pc"/>
		<aop:after-throwing method="afterException" pointcut-ref="pc"/>
		<aop:after method="after" pointcut-ref="pc"/>
		</aop:aspect>
	</aop:config>
</beans>

spring 整合jdbc和spring事物管理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
	<!-- 1将连接池放入spring容器 -->
	<context:property-placeholder location="classpath:db.properties" />
	<!-- 事物核心管理器 -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
	<property name="dataSource" ref="dataSource" ></property>
    </bean>
	<!-- 配置事物通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED"
				read-only="false" />
			<tx:method name="persist*" isolation="DEFAULT" propagation="REQUIRED"
				read-only="false" />
			<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED"
				read-only="false" />
			<tx:method name="modify*" isolation="DEFAULT" propagation="REQUIRED"
				read-only="false" />
			<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED"
				read-only="false" />
			<tx:method name="remove*" isolation="DEFAULT" propagation="REQUIRED"
				read-only="false" />
			<tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED"
				read-only="true" />
			<tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED"
				read-only="true" />
			<tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED"
				read-only="false" />
		</tx:attributes>
	</tx:advice>
	<!-- 配置织入 -->
	<aop:config>
		<aop:pointcut expression="execution(* lx.service.impl.*ServiceImpl.*(..))"
			id="txpc" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txpc" />
	</aop:config>
	<beans>
		<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
			<property name="driverClass" value="${jdbc.driverClass}"></property>
			<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
			<property name="user" value="${jdbc.user}"></property>
			<property name="password" value="${jdbc.password}"></property>
		</bean>
		<!-- 2将jdbctemple放入spring容器 -->
		<bean name="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
			<property name="dataSource" ref="dataSource"></property>
		</bean>
		<!-- 3将userDao放入spring容器 -->
		<bean name="userDao" class="lx.dao.impl.UserDaoImpl">
			<property name="jt" ref="JdbcTemplate"></property>
		</bean>
		<bean name="accountDao" class="lx.dao.impl.AccountDaoImpl">
			<property name="dataSource" ref="dataSource"></property>
		</bean>
		<bean name="accountService" class="lx.service.impl.AccountServiceImpl">
			<property name="ad" ref="accountDao"></property>
		</bean>
	</beans>
</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值