spring核心知识点(个人理解)

    spring是什么?spring是一个容器!什么的容器?对象的容器!!!这个对于刚刚接触spring的人(例如我)理解起来比较抽象。简单来说,就是你想要实现什么样的功能,但是你又懒,你不想写实现类,这个时候,容器的概念就更加明白了,就是别人写好的东西,我把它放在自己的spring配置文件中,然后我就可以使用别人的功能了。当然自己写的也可以放进去,供自己用。

    应该说读这篇文章的都是简单的了解spring的人了吧!都知道spring的两大撒手锏武器就是DI(依赖注入)和IOC(控制反转)。

    什么是DI?就是你可以在利用spring配置文件获取对象的时候就把对象改造成你想要的样子,而且<bean>之间是可以有关系的,而不是独立的!!!

    什么是IOC呢?简单来说,就是你把对象的创建权限交给你的管家--spring了,你不负责这个过程,而是当你需要的时候,向管家要!!!

    spring的起步阶段需要注意的几个知识点:

        1    spring环境的搭建:需要几个基础jar包:beans,expression,core,context,外加logging,log4j


        2    配置文件的读取


这个写得很不规范,大家注意,只是为了表达原理。除此之外,途中new的返回值类型也需要注意(配置destor方法时),这个点一下!

        3    <bean>标签的属性,简单的说一下,就是怎么配置对象?怎么注入属性?如果属性是复杂类型又该怎么注入?


简单的截个图,供大家理解

        4    通知的理解及具体操作/////重点///插入代码吧,

//advice.java
package com.axis.advice;

import org.apache.tomcat.jni.ProcErrorCallback;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;


@Component(value="advicer")
public class Advicer {
public void before() {
	System.out.println("before");
}
public void after() {
	System.out.println("after");
}

public void around(ProceedingJoinPoint pc) throws Throwable {
	System.out.println("around before");
	Object object = pc.proceed();
	System.out.println(object);
	System.out.println("around after");
}
}
//ServiceDaoImpl.java
package com.axis.service;

import org.springframework.stereotype.Component;

@Component(value="serviceDaoImpl")
public class ServiceDaoImpl implements ServiceDao {

	@Override
	public void serSave() {
		System.out.println("save");
	}

	@Override
	public void serDel() {
		System.out.println("del");
	}

	@Override
	public void serUpdate() {
		System.out.println("update");
	}

	@Override
	public void serFind() {
		System.out.println("find");
	}
}

//a.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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="advicer" class="com.axis.advice.Advicer"></bean>
	<bean name="serviceDaoImpl"
		class="com.axis.service.ServiceDaoImpl"></bean>
	<context:component-scan
		base-package="com.axis.service"></context:component-scan>
	<context:component-scan
		base-package="com.axis.advice"></context:component-scan>
	<aop:config>
		<aop:pointcut
			expression="execution(* com.axis.service.ServiceDaoImpl.ser*(..))"
			id="pc" />
		<aop:aspect ref="advicer">
			<aop:before method="before" pointcut-ref="pc" />
			<aop:after method="after" pointcut-ref="pc" />
			<aop:around method="around" pointcut-ref="pc" />
		</aop:aspect>
	</aop:config>
</beans>
//Demo
package com.axis.service;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.stereotype.Component;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@Component("hello")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:com/axis/service/a.xml")
public class Demo {
	@Resource(name = "serviceDaoImpl")
	private ServiceDao s;

	@Test
	public void test() {
		s.serSave();
	}
}

代码涉及需要导入的jar包,这个在之前的图有体现,可以参考以下:主要是aspect,aop,test


猜你喜欢

转载自blog.csdn.net/jokerlance/article/details/81046319
今日推荐