1、IOC
inversion of control
之前写的忘记保存了。。呜呜呜。。
<dependency>
<groupId>org.springframework</groupId>
<artifactId> spring-context</artifactId>
<version>5.1.0.BUILD-SNAPSHOT</version>
</dependency>
package com.spring.dao;
public interface UserDao {
void getUser();
}
package com.spring.dao;
public class UserMysqlImpl implements UserDao {
public void getUser() {
System.out.println("mysql");
}
}
package com.spring.dao;
public class UserOracleImpl implements UserDao{
public void getUser() {
System.out.println("oracle");
}
}
package com.spring.service;
import com.spring.dao.UserDao;
public class UserServiceImpl implements UserService{
private UserDao userDao ;
public UserDao getUserDao() {
return userDao;
}
//利用set进行动态值注入
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public void getUser() {
userDao.getUser();
}
}
@Test
public void test1(){
// 获取spring容器
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
// 直接从spring中获取对象
UserServiceImpl serviceImpl = (UserServiceImpl) context.getBean("serviceImpl");
serviceImpl.getUser();
}
<bean id="MysqlImpl" class="com.spring.dao.UserMysqlImpl" name="mysql"/><!-- name别名 -->
<bean id="OracleImpl" class="com.spring.dao.UserOracleImpl"/>
<bean id="serviceImpl" class="com.spring.service.UserServiceImpl">
<property name="userDao" ref="OracleImpl"/>
</bean>
1、依赖注入
2、p命名空间,c命名空间
需要导入xml约束
3、bean的作用域
2、Bean的自动装配
<bean id="cat" class="com.spring.pojo.Cat"/>
<bean id="dog" class="com.spring.pojo.Dog"/>
<bean id="people" class="com.spring.pojo.People">
<property name="cat" ref="cat"/>
<property name="dog" ref="dog"/>
<property name="name" value="www"/>
</bean>
1、autowire ByName和ByType
2、使用注解实现自动装配
context:annotation-config/
@Autowired
3、注解开发
@Component
4、java方式配置spring
@Component
public class User {
private String name;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
'}';
}
public String getName() {
return name;
}
@Value("dsds")
public void setName(String name) {
this.name = name;
}
}
5、代理模式
1、静态代理
抽象角色
package com.spring.demo;
//租房
public interface Rent {
public void rent();
}
真是角色
package com.spring.demo;
public class Host implements Rent {
public void rent() {
System.out.println("房东要出租房子");
}
}
代理角色
package com.spring.demo;
public class Proxy implements Rent{
private Host host;
public Proxy() {
}
public Proxy(Host host) {
this.host = host;
}
public void rent() {
host.rent();
}
//附属功能
public void fare(){
}
}
客户
public class Client {
public static void main(String[] args) {
Host host = new Host();
Proxy proxy = new Proxy(host);
proxy.rent();
}
}
2、动态代理
总结:要使用动态代理,用来生成代理的委托对象必须是一个接口。
public class ProxyDy implements InvocationHandler {
// 被代理的接口
private Object target;
public void setTarget(Object target) {
this.target = target;
}
//生成得到代理类
public Object getProxy(){
return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
}
// 处理代理实例,返回结果
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(target,args);
}
}
public class Client {
public static void main(String[] args) {
Host host = new Host();
ProxyDy proxyDy = new ProxyDy();
proxyDy.setTarget(host);
Rent proxy = (Rent) proxyDy.getProxy();
proxy.rent();
}
}
6、AOP
方式三:使用注解方式使用Aop
1、spring的api实现aop
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:aop = "http://www.springframework.org/schema/aop"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<!-- bean definition & AOP specific configuration -->
<bean id="before" class="com.spring.log.beforeLog"/>
<bean id="after" class="com.spring.log.afterLog"/>
<bean id="userService" class="com.spring.service.UserviceImpl"/>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.spring.service.UserviceImpl.*(..))"/>
<aop:advisor advice-ref="before" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="after" pointcut-ref="pointcut"/>
</aop:config>
</beans>
2、自定义实现aop(自定义切面)
package com.spring.diyAop;
public class DiyAspect {
public void before(){
System.out.println("before");
}
public void after(){
System.out.println("after");
}
}
xml
<bean id="diy" class="com.spring.diyAop.DiyAspect"/>
<aop:config>
<!-- 自定义一个切面-->
<aop:aspect ref="diy">
<aop:pointcut id="pointct" expression="execution(* com.spring.service.UserServiceImpl.*(..))"/>
<aop:before method="before" pointcut-ref="pointct"/>
<aop:after method="after" pointcut-ref="pointct"/>
</aop:aspect>
</aop:config>
3、使用注解实现aop
package com.spring.annoAop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect//标注这是一个切面
public class annoAop {
@Before("execution(* com.spring.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("before");
}
@After("execution(* com.spring.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("after");
}
}
<!-- 方式三-->
<bean id="annoAop" class="com.spring.annoAop.annoAop"/>
<!-- 开启注解支持-->
<aop:aspectj-autoproxy/>
7、mybatis-spring
1、SqlSessionTemplate
spring-dao.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:aop = "http://www.springframework.org/schema/aop"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- spring管理数据源替换mybatis的数据源配置-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncode=utf8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- \SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--扫描映射文件-->
<!-- <property name="mapperLocations" value="classpath:com/spring/mapper/*.xml"/>-->
</bean>
<!-- 原来是使用sqlSession ,现在是SqlSessionTemplate-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 构造器注入-sqlSessionFactory-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
applicationContext.xml
<import resource="spring-dao.xml"/>
<bean id="UserMapper" class="com.spring.mapper.UserMapperImpl">
<property name="sqlSession" ref="sqlSession"/>
</bean>
MyTest
import com.spring.mapper.UserMapper;
import com.spring.pojo.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = context.getBean("UserMapper", UserMapper.class);
List<User> users = mapper.selectUser();
for (User user : users) {
System.out.println(user);
}
}
}
2、SqlSessionDaoSupport
<bean id="UserMapper2" class="com.spring.mapper.UserMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
public class UserMapperImpl2 extends SqlSessionDaoSupport implements UserMapper {
public List<User> selectUser() {
return getSqlSession().getMapper(UserMapper.class).selectUser();
}
}
public class MyTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserMapper mapper = context.getBean("UserMapper2", UserMapper.class);
List<User> users = mapper.selectUser();
for (User user : users) {
System.out.println(user);
}
}
}
8、声明式事务
<!-- 配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 结合AOP实现事务的织入-->
<!-- 事务的通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 那些方法配置事务-->
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="select*" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 事务切入-->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.spring.mapper.UserMapperImpl.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
public class UserMapperImpl implements UserMapper {
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
public void addUser(User user) {
sqlSession.getMapper(UserMapper.class).addUser(user);
}
public void deleteUser(int id) {
sqlSession.getMapper(UserMapper.class).deleteUser(1);
}
public List<User> selectUser() {
addUser(new User(11,"jkjknh","kewjekwj"));
deleteUser(1);
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
return userMapper.selectUser();
}
}