Spring小记

 2023-09-11 阅读 18 评论 0

摘要:Spring小记从IOC容器获取bean复杂类型注入一个接口有多个实现类,如何自动装配AOPJdbc Tempalte 从IOC容器获取bean ClassPathXmlApplicationContext(从类路径获取文件) 和 FileSystemXmlApplicationContext(从系统磁盘径获取文件)。 方式

Spring小记

    • 从IOC容器获取bean
    • 复杂类型注入
    • 一个接口有多个实现类,如何自动装配
    • AOP
    • Jdbc Tempalte

从IOC容器获取bean

ClassPathXmlApplicationContext(从类路径获取文件) 和 FileSystemXmlApplicationContext(从系统磁盘径获取文件)。

方式一(ClassPathXmlApplicationContext(从类路径获取文件)方式):
实体类:

public class Person {private String name;private Integer age;public Person() {super();System.out.println("构造方法被调用");}//getters、setters、toString
}

applicationContext.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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- bean:使容器创建Person的对象 --><!-- name:相当于变量名 Person p = new Person(); --><!-- class:类的全限定名 -->     <bean name="p" class="com.qfedu.spring.pojo.Person"></bean>   
</beans>

获取bean测试:

public class IOCTest {@Testpublic void testCreatePerson() {//创建容器ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");//查找对象Person p = (Person)context.getBean("p");System.out.println(p);}
}

方式二(FileSystemXmlApplicationContext(从系统磁盘径获取文件)方式):

 @Testpublic void ApplicationContext() {    //创建容器ApplicationContext context = new FileSystemXmlApplicationContext("D:\\workspace_helen\\web-gp1701\\spring-01-hello\\src\\applicationContext.xml");}

复杂类型注入

    private Object[] cars;//数组类型的注入private List friends; //list类型的注入private Set set;//set类型的注入private Map map;//map类型的注入private Properties properties;//properties类型的注入
<bean name="person5" class="com.qfedu.spring.pojo.Person"><property name="name" value="houkunpeng"></property><property name="age" value="16"></property><property name="car" ref="car"></property><property name="cars"><array><value>自行车</value><value>手扶拖拉机</value><ref bean="car" /></array></property><property name="friends"><list><value>洋妞红</value><value>溜冰摔</value><value>少元彬</value></list></property><property name="set"><set><value>set3</value><value>set2</value><value>set1</value></set></property><property name="map"><map><entry key="username" value="root"></entry><entry key="car" value-ref="car"></entry><entry key-ref="person1" value-ref="car"></entry></map></property><property name="properties"><props><prop key="username">root</prop><prop key="password">123456</prop></props></property></bean>

一个接口有多个实现类,如何自动装配

自动装配存在的问题:如果一个类型有多个对象(一个接口有多个实现类),那么可以采用以下的方式:
TestService接口:

public interface TestService {public void getMessage();
}

TestServiceImpl实现TestService:

@Service
public  class TestServiceImpl implements TestService {@Overridepublic void getMessage() {System.out.println("test serviceImpl one");}
}

TestServiceImpl2实现TestService:

@Service
public class TestServiceImpl2 implements TestService {@Overridepublic void getMessage() {System.out.println("test serviceImpl tow");}
}

测试类:
解决装配起义性的方式有三种

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
class TestServiceImplTest {// 方式一
//    @Autowired // 按照byType自动装配
//    @Qualifier("testServiceImpl") // 按照byName,类名首字母小写// 方式二//   @Resource(type = com.xmm.springboot_lab.service.impl.TestServiceImpl.class)// 按照byType// 方式三@Resource(name = "testServiceImpl2") //按照byName,类名首字母小写private TestService testService;@Testvoid getMessage() {testService.getMessage();}
}

AOP

aop和代理:blog.csdn.net/qq_41701956/article/details/84427891

AOP是能够让我们在不影响原有功能的前提下,为软件横向扩展功能。

方式一
aop通过xml方式:

public class UserServiceImpl implements UserService{@Overridepublic void save() {System.out.println("保存用户");}@Overridepublic void delete() {System.out.println("删除用户");}@Overridepublic void update() {System.out.println("更新用户");}@Overridepublic void select() {System.out.println("查询用户");}
}

切面类:

public class TransactionAdvice {
//    前置通知:在目标方法之前调用
//    后置通知(如果出现异常就不调用):在目标方法之后调用
//    返回通知(无论是否出现异常都会调用):在目标方法之后调用
//    环绕通知:在目标方法之前、后调用
//    异常通知:出现异常则调用public void before() {System.out.println("前置通知被执行");}public void afterReturning() {System.out.println("后置通知被执行(出现异常不调用)");}public void after() {System.out.println("后置通知被执行(无论是否出现异常都会调用)");}public void afterException() {System.out.println("异常通知被执行");}public Object around(ProceedingJoinPoint point) throws Throwable {System.out.println("");Object proceed = point.proceed();//调用目标方法System.out.println("");return proceed;}
}

xml配置:

<!-- 目标对象 -->   <bean name="userService" class="com.qfedu.spring.aop.service.UserServiceImpl"></bean><!-- 通知对象 --><bean name="transactionAdvice" class="com.qfedu.spring.aop.advice.TransactionAdvice"></bean><!-- 将通知对象织入目标对象 --><aop:config><!-- 选择切入点 --><aop:pointcut expression="execution(* com.qfedu.spring.aop.service..*ServiceImpl.*(..))" id="pointcut"/><aop:aspect ref="transactionAdvice"><aop:before method="before" pointcut-ref="pointcut"/><aop:after-returning method="afterReturning" pointcut-ref="pointcut"/><aop:after method="after" pointcut-ref="pointcut"/><!-- 无论是否出现异常都会调用 --><aop:around method="around" pointcut-ref="pointcut"/><aop:after-throwing method="afterException" pointcut-ref="pointcut"/></aop:aspect></aop:config>

测试:

//创建容器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {@Resource(name="userService")private UserService userService;@Testpublic void testSave() {userService.save("helen");}  @Testpublic void testUpdate() {userService.update();}
}

方式二
aop通过注解方式:

public class UserServiceImpl implements UserService{@Overridepublic void save() {System.out.println("保存用户");}@Overridepublic void delete() {System.out.println("删除用户");}@Overridepublic void update() {System.out.println("更新用户");}@Overridepublic void select() {System.out.println("查询用户");}
}

切面类:

@Aspect
public class TransactionAdvice {@Pointcut("execution (* com.qfedu.spring.aop.service..*ServiceImpl.*(..))")private void anyMethod() {} // 声明一个切入点,anyMethod为切入点名称// 声明该方法是一个前置通知:在目标方法开始之前执行 @Before("anyMethod()")public void doAccessCheck() {System.out.println("前置通知被执行");}
}

xml配置:

 <!-- 目标对象 -->   <bean name="userService" class="com.qfedu.spring.aop.service.UserServiceImpl"></bean><!-- 通知对象 --><bean name="transactionAdvice" class="com.qfedu.spring.aop.advice.TransactionAdvice"></bean><!-- 开启织入注解 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy>

测试:

/创建容器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {@Resource(name="userService")private UserService userService;@Testpublic void testSave() {userService.save("helen");}@Testpublic void testUpdate() {userService.update();}
}

Jdbc Tempalte

实体类:

public class Role {private Integer rid;private String rname;private String alias;//空参构造、getters、setters@Overridepublic String toString() {return "Role [rid=" + rid + ", rname=" + rname + ", alias=" + alias + "]";}
}

dao接口:

package com.qfedu.spring.dao;
public interface RoleDao {void save(Role role);void delete(Integer id);void update(Role role);Role getById(Integer id);List<Role> getAll();int getTotalCount();
}

实现dao接口:

package com.qfedu.spring.dao;
public class RoleDaoImpl implements RoleDao{JdbcTemplate jt;public void setJt(JdbcTemplate jt) {this.jt = jt;}@Overridepublic void save(Role role) {String sql = " INSERT INTO ar_role ( "+ " rname,"+ " alias"+ " ) VALUES (?,?)" ;jt.update(sql, role.getRname(), role.getAlias());}@Overridepublic void delete(Integer id) {String sql = " delete from ar_role where rid = ?" ;jt.update(sql, id);}@Overridepublic void update(Role role) {String sql = " update ar_role set rname = ?, alias = ? where id = ?";jt.update(sql, role.getRname(), role.getAlias(), role.getRid());}@Overridepublic Role getById(Integer id) {String sql = "select * from ar_role where rid = ?";Role role = jt.queryForObject(sql, new RowMapper<Role>() {@Overridepublic Role mapRow(ResultSet rs, int index) throws SQLException {return maoRowHandler(rs);}}, id);return role;}@Overridepublic List<Role> getAll() {String sql = "select * from ar_role";List<Role> list = jt.query(sql, new RowMapper<Role>() {@Overridepublic Role mapRow(ResultSet rs, int index) throws SQLException {return maoRowHandler(rs);}});return list;}@Overridepublic int getTotalCount() {String sql = "select count(1) from ar_role";Integer count = jt.queryForObject(sql, Integer.class);return count;}private Role maoRowHandler(ResultSet rs) throws SQLException {Role role = new Role();role.setRname(rs.getString("rname"));role.setAlias(rs.getString("alias"));role.setRid(rs.getInt("rid"));return role;}
}

创建db.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring-gp1701
jdbc.username=root
jdbc.password=123456

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"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><context:property-placeholder location="classpath:db.properties" />        
<!-- 连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.password}"></property>
</bean>
<!-- JdbcTemplate -->
<bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"></property>
</bean>
<!-- roleDao -->
<bean name="roleDao" class="com.qfedu.spring.dao.RoleDaoImpl"><property name="jt" ref="jdbcTemplate"></property>
</bean>
</beans>

测试:

package com.qfedu.spring.jdbctemplate;
//创建容器
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestSpringJdbcTemplate {@Resource(name="roleDao")private RoleDao roleDao;@Testpublic void testSave(){Role role = new Role();role.setRname("spring1");role.setAlias("spring11");roleDao.save(role);}@Testpublic void testDelete(){roleDao.delete(5);}@Testpublic void testUpdate(){Role role = new Role();role.setRname("spring4");role.setAlias("spring44");role.setRid(4);roleDao.update(role);}@Testpublic void testGetById(){Role role = roleDao.getById(4);System.out.println(role);}@Testpublic void testGetAll(){List<Role> roleList = roleDao.getAll();System.out.println(roleList);}@Testpublic void testGetTotalCount(){Integer count = roleDao.getTotalCount();System.out.println(count);}
}

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/47676.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息