ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring简单使用和配置

2021-02-27 16:31:08  阅读:236  来源: 互联网

标签:xml spring 配置 jar pointcut 简单 RELEASE public


Spring

核心jar

spring-beans-x.x.x.RELEASE.jar
spring-context-x.x.x.RELEASE.jar
spring-core-x.x.x.RELEASE.jar
commons-logging-x.x.x.jar
spring-expression-4.1.2.RELEASE.jar

在资源文件夹中创建一个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/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
	<bean id="别名:唯一标识" class="类的全限定名">
	<!-- collaborators and configuration for this bean go here -->
	</bean>
</beans>

xml其他配置属性

<beans default-lazy-init="true"</beans> <!--全局懒加载-->
 <!--这个类开启懒加载模式 lazy-init="true" -->
<bean id="别名:唯一标识" class="类的全限定名" lazy-init="true">
<!--bean中的初始属性   scope="prototype"多对象 -->
<bean id="别名:唯一标识" class="类的全限定名" scope="prototype">
     <property name="类中的字段名" ref="值"></property>//需要可读可写属性
</bean>

依赖注入

管理对象:

通过注解@Component注入

通过xml中bean标签管理

使用注解获取实例:(不是在spring测试类需要扫描全局配置):

​ @Autowired//默认按照名称,名称找不到类型匹配

​ @Qualifier(“otherBean1”)//可以使用这个注解指定Bean的名称

​ @Resource 默认按照名字匹配【名字对了,类型也必须一致】,然后按照类型匹配

​ @Resource(name=“otherBean1”)//指定Bean的名称

使用核心对象获取实例
//第一个种方法(过时)
	//第一步:读取资源文件
	Resource resource = new ClassPathResource("applicationContext.xml");
	//第二步:拿到核心对象 BeanFactory
	BeanFactory factory = new XmlBeanFactory(resource);

//第二种方法
	ApplicationContext factory = new ClassPathXmlApplicationContext ("applicationContext.xml");

//xml注入
	//直接使用别名
	Object bean = factory.getBean("xml中别名");
	//直接字节码文件
	类 bean = factory.getBean(类.class);
	//使用字节码对象和别名(推荐)
	类 bean = factory.getBean("myBean",类.class);

ApplicationContext与BeanFactory的区别:

​ ApplicationContext是迫切加载,加载Spring的时候就创建bean的一个对象.BeanFactory只有获取对象的时候才创建.

使用xml:

<bean id="别名:唯一标识" class="类的全限定名">

构造器注入

通过有参构造管理类.

第一种通过索引

<bean id="类名小写首字母" class="类的完全限定名">
	<constructor‐arg index="0" value="小赤佬" />
	<constructor‐arg index="1" value="12" />
</bean>
<!-- index 参数的索引值 value 传入的值-->

第二种名称

<bean id="类名小写首字母" class="类的完全限定名">
	<constructor‐arg name="name" value="小赤佬" />
	<constructor‐arg name="age" value="12" />
</bean>
<!-- name形参变量名-->

第三种根据类

<bean id="类名小写首字母" class="类的完全限定名">
    <constructor‐arg type="java.lang.String" value="小赤佬" />
    <constructor‐arg type="java.lang.Integer" value="12" />
</bean>
值为类的时候:外部引入和内部引入类
<bean id="bean" class="cn.xxx.Bean" />
<bean id="类名小写首字母" class="类的完全限定名">
	<constructor‐arg index="0" value="小赤佬" />
	<constructor‐arg index="1" value="12" />
    <!--内部创建-->
    <constructor‐arg index="2">
    	<bean id="bean" class="cn.xxx.Bean" />
    </constructor‐arg>
    <!--外部引入-->
    <constructor‐arg index="3" ref="bean" />
    <!--一个bean标签就创建一个实例对象 -->
</bean>
数组,集合等注入
<bean id="类名小写首字母" class="类的完全限定名">
	<constructor‐arg index="0" value="小赤佬" />
	<constructor‐arg index="1" value="12" />
    <constructor‐arg index="2">
        <!--数组 set list 一致只是标签换成set list-->
    	<array>
        	<value>字符串</value>
            <!-- 类 -->
            <bean id="bean" class="cn.xxx.Bean" />
            <ref bean="bean"/>
        </array>
        
        <!--map-->
        <map>
            <entry key="xx"></entry>
        </map>
    </constructor‐arg>
</bean>

Spring测试

​ 需要的jar包

spring-test-x.x.x.RELEASE.jar		--	测试包
spring-aop-x.x.x.RELEASE.jar 		--	AOP包
@RunWith(SpringJUnit4ClassRunner.class)     //启用spring,把junit运行在spring容器中 
@ContextConfiguration("classpath:applicationContext.xml")
//ContextConfiguration:加载配置文件
//classpath:表示在编译根目录下寻找
//不写:在同级文件下寻找 测试类名-Context.xml 的配置文件
//不加classpath:在类当前下的包中寻找
public class 测试类名{
   	@Autowired//注解:自动依赖
	private ApplicationContext context;
}

AOP切面编程

xml版本

<?xml version="1.0" encoding="UTF‐8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema‐instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring‐beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring‐aop.xsd
">
	<!‐‐准备相应的事务对象‐‐>
	<bean id="txManager" class="全限定名" />
	<bean id="userService" class="全限定名" />
	<!‐‐代表这里是一个aop的配置‐‐>
	<aop:config>
		<!‐‐切点:找到我们要在哪里加断点‐‐>
		<aop:pointcut id="pointcut" expression="execution(* 全限定名.*(..))" />
		<!‐‐把事务对象放到咱们的切面中‐‐>
		<aop:aspect ref="txManager">
			<!‐‐前置通知‐‐>
			<!‐‐<aop:before method="begin" pointcut‐ref="pointcut" />‐‐>
			<!‐‐后置通知‐‐>
			<!‐‐<aop:after‐returning method="commit" pointcut‐ref="pointcut" />‐‐>
			<!‐‐异常通知‐‐>
			<!‐‐<aop:after‐throwing method="rollback" pointcut‐ref="pointcut" />‐‐>
			<!‐‐最终通知‐‐>
			<!‐‐<aop:after method="close" pointcut‐ref="pointcut" />‐‐>
            <!--使用的环绕通知就最好不使用其他通知-->
			<!‐‐环绕通知‐‐>
			<aop:around method="around" pointcut‐ref="pointcut" />
		</aop:aspect>
	</aop:config>
</beans>

注解配置

​ xml配置

<context:component‐scan base‐package="要扫包的路径" />
<!‐‐支持aop的注解‐‐>
<aop:aspectj‐autoproxy />

java实现类

/**
* 事务对象
*/
@Component
@Aspect
public class TxManager {
	//切点的配置
	@Pointcut("execution(* 全限定名.*(..))")
	public void pointcut(){}
	//前置通知
	// @Before("pointcut()")
	public void begin(){}
	//后置通知
	// @AfterReturning("pointcut()")
	public void commit(){}
	//异常通知
	// @AfterThrowing("pointcut()")
	public void rollback(){}
	//最终通知
	// @After("pointcut()")
	public void close(){}
	@Around("pointcut()")
	public Object around(ProceedingJoinPoint joinPoint){
		try {
			begin();
			object = joinPoint.proceed(); //执行相应的代码
			commit();
		} catch (Throwable e) {
			rollback();
		}finally{
			close();
		}
		return object;
	}
}

支持事务

依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring‐tx</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>

注解版事务xml配置

<context:component‐scan base‐package="全限定名" />
    //...其它配置与扫描
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
   		 <property name="dataSource" ref="dataSource" />
    </bean>
<tx:annotation‐driven />

使用注解@Transactional 就添加事务

xml版事务

配置xml

//事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource" />
</bean>

//事务属性
<tx:advice id="txAdvice" transaction‐manager="transactionManager">
    <tx:attributes>
    <!‐‐propagation:事务传播机制为支持 read‐only:只读 ‐‐>
    	<tx:method name="find*" propagation="SUPPORTS" read‐only="true" />
    	<tx:method name="get*" propagation="SUPPORTS" read‐only="true" />
    	<tx:method name="select*" propagation="SUPPORTS" read‐only="true" />
    	<tx:method name="*"/>
    </tx:attributes>
</tx:advice>

//配置切入点
<aop:config>
	<aop:pointcut expression="execution(* 全限定名.*(..))" id="pointcut"/>
	<aop:advisor advice‐ref="txtAdvice" pointcut‐ref="pointcut"/>
</aop:config>

标签:xml,spring,配置,jar,pointcut,简单,RELEASE,public
来源: https://blog.csdn.net/kochiya_/article/details/114178418

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有