spring中的注解和xml配置⽂件中配置对应总结spring中的注解和xml配置⽂件中配置对应
需要导⼊的jar
spring-context
spring-context-support
spring-test
commons-logging
bean
xml⽂件中的配置
resource和autowired注解的区别id:对象名字唯⼀,不能使⽤特殊字符
name:对象名字
class:类的全限定名 包名.类名
init-method:初始化⽅法
destroy-method:销毁对象之前执⾏的⽅法
scope:作⽤域范围
prototype:多例
创建⼀次是⼀个新的对象
singleton:单例(懒汉式 饿汉式)
不管如何创建,只能是⼀个对象
lazy-init:是否延迟加载当前对象
只在单例模式下有效(多例模式下每次都需要调⽤⽆参构造对象,所以⽆效)
1.false ⽴即加载 加载当前spring配置⽂件时就创建对象
ref:引⼊参照对象
property ⽆参构造
name:对象中属性的名字
value:赋的值
constructor-arg有参构造
name注⼊:属性名
type注⼊:属性名的类型
index注⼊:类中第⼀个属性名为下标为 0
<bean id="provincesId"
name="provinces"
class="com.lanou.pojo.Provinces"
init-method="init"
destroy-method="destroy"
scope="singleton"
lazy-init="true"
>
<!--利⽤setter⽅法给对应的属性赋值-->
<property name="id"value="1"></property>
<property name="province"value="北京市"></property>
<property name="provinceid"value="110000"></property>
</bean>
<bean id="user"class="com.lanou.pojo.User"scope="singleton"lazy-init="true"> <constructor-arg name="id"value="123"type="int"></constructor-arg>
<constructor-arg name="username"value="说爱你"type="java.lang.String"></constructor-arg> <constructor-arg name="password"value="love"type="java.lang.String"></constructor-arg> <!--<constructor-arg index="0" value="121"></constructor-arg>-->
<!--<constructor-arg index="1" value="我爱你"></constructor-arg>-->
<!--<constructor-arg index="2" value="sss"></constructor-arg>-->
</bean>
复杂数据类型配置
<bean id="testCollection"class="com.lanou.pojo.TestCollection">
<!--对象类型 ref引⼊参照对象-->
<property name="user"ref="user"></property>
<!--数组-->
<property name="array">
<list>
<value>郑州</value>
<value>123</value>
<value>0.12</value>
</list>
</property>
<!--集合类型list-->
<property name="list">
<list>
<value>郑州</value>
<value>洛阳</value>
<value>开封</value>
</list>
</property>
<!--集合类型map-->
<property name="map">
<map>
<entry key="area"value="河南"></entry>
<entry key="fruit"value="芒果"></entry>
<entry key="user"value-ref="user"></entry>
</map>
</property>
<!--Properties类型赋值 key=value-->
<property name="properties">
<props>
<prop key="driver"&sql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost/mybatis</prop>
<prop key="username">root</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
bean的注解
开启注解
<!--可省略不写开启spring注解功能-->
<annotation-config></annotation-config>
<!--定义注解扫描的包-->
<component-scan base-package="com.lanou"></component-scan>
配置类
@Component 普通的javabean pojo包,util包
@Scope 单例多例模式值()
@Lazy延迟加载
配置属性赋值
@Value(value = “123”)
配置⽅法
@PostConstruct 初始化注解
@PreDestroy 销毁对象之前执⾏的⽅法
@Component(value ="user")
@Scope
public class User {
@Value(value ="123")
private int id;
@Value("张三")
private String username;
@Value("123456")
private String password;
层与层之间调⽤
xml配置
<bean id="provincesService"class="com.lanou.service.serviceImpl.ProvincesServiceImpl"></bean>
<bean id="provincesServlet"class="com.lanou.servlet.ProvincesServlet">
<property name="id"value="100"></property>
<property name="provincesService"ref="provincesService"></property>
</bean>
注解
控制层上注解类上⾯使⽤
@Controller(value = “userController”)
业务层类上使⽤的注解
@Service(“userService”)
属性上使⽤的注解
@Autowired //既可以通过类型,也可以通过名称注⼊
@Qualifier(value = “userDao2”)// 两个注解联合使⽤时,强制指定通过名称注⼊@Resource(name = “userDao2”)
持久层类使⽤的注解
@Repository(“userDao”)
@RunWith(SpringJUnit4ClassRunner.class) 创建sprng容器
@ContextConfiguration(“l”) 指定配置⽂件位置ApplicationContext context =new ClassPathXmlApplicationContext("l");
aop ⾯向切⾯编程
xml⽂件中的配置
aop:config 定义切⼊点
aop:pointcut 切⼊点
id : 名称
expression : 表达式 到包、类、⽅法
execution(* com.lanou.service..(…))
第⼀个* ⽅法的返回值类型
com.lanou.service 包名
第⼆个* 类名
第三个* ⽅法名
(…) 任意参数
aop:aspect 定义关联的切⾯
aop:before 前置通知
method:切⾯中的前置⽅法
pointcut-ref:切⼊点
aop:after 后置通知
aop:after-returning :后置通知 如果异常不返回值
aop:after-throwing :后置通知 异常返回
aop:around :环绕通知
<!--定义切⾯信息-->
<bean id="logAdvice"class="com.lanou.utils.LogAdvice2"></bean>
<!--定义切⼊点-->
<config>
<pointcut id="pc"expression="execution(* com.lanou.service.*.*(..))"/>
<!--定义关联的切⾯-->
<aspect ref="logAdvice">
<before method="before"pointcut-ref="pc"></before>
<after method="after"pointcut-ref="pc"></after>
<after-returning method="afterReturning"pointcut-ref="pc"></after-returning>
<after-throwing method="afterException"pointcut-ref="pc"></after-throwing>
<around method="around"pointcut-ref="pc"/>
</aspect>
</config>
注解中的配置
导⼊aop需要的jar包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.14.RELEASE</version>
</dependency>
<!-- mvnrepository/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
@Aspect:声明当前类是⼀个切⾯类
@Pointcut(“execution(* com.lanou.service..(…))”):切⼊点
@Before(“LogAdvice.pc()”): 前置通知⽅法
@After(“execution(* com.lanou.service..(…))”):后置通知⽅法 不管是否异常都返回@AfterReturning(“execution(* com.lanou.service..(…))”):后置通知 如果异常不返回值@AfterThrowing(“execution(* com.lanou.service..(…))”):后置通知 异常返回
<!--开启aop注解-->
<aspectj-autoproxy></aspectj-autoproxy>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论