SpringBoot(一)

Spring的基礎配置的四大原則

  1. 使用POJO進行輕量級和最小侵入式開發
  2. 經過依賴注入和基於接口編程實現鬆耦合
  3. 經過AOP和默認習慣進行聲明式編程
  4. 使用AOP和模板(template)減小模式化代碼

聲明bean的註解java

  • @Component組件,沒用明確的角色
  • @Service在業務邏輯層(service層)使用
  • @Repository在數據訪問層訪問(dao層)使用
  • @Controller在展示層(MVC->Spring MVC)使用 注入Bean的註解,通常狀況下是通用的
  • @Autowared:Spring提供的註解
  • @Inject: JSR-330提供的註解
  • @Resource: JSR-250提供的註解

例子: FunctionService.java數據庫

@Service
public class FunctionService {
	public String sayHello(String content){
		return "Hello "+content;
	}
}

UseFunctionService.java編程

@Service
public class UseFunctionService {
@Autowired
	FunctionService functionService;
public String sayHello(String content){
	return functionService.sayHello(content);
}
}

DiConfig.java(配置類,做爲元數據)測試

@Configuration
@ComponentScan("com.flexible")//掃描包
public class DiConfig {
}

測試代碼flex

AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(DiConfig.class);
		UseFunctionService u = configApplicationContext.getBean(UseFunctionService.class);
		String test = u.sayHello("test");
		System.out.println(test);

執行結果:.net

Java配置

Java配置是Spring4.x推薦的配置方式,這種配置方式徹底能夠替代xml配置,Java配置也是SpringBoot推薦的配置方式。從上面的例子能夠知道Java配置使用@Configuration和@Bean來實現。@Configuration聲明一個類至關於S的xml配置文件,使用@Bean註解在方法上,聲明當前方法的返回值作爲一個Bean.3d

使用Java配置和註解配置的原則

全局配置使用Java配置(如數據庫的相關配置,MVC香關配置),業務Bean的配置使用註解配置(@Service,@Compoent,@Repository,@Controller)code

AOP配置

SpringAOP存在目的就是爲了給程序解耦,AOP能夠讓一組類共享相同的行爲,實現了OOP沒法實現的一些功能,在必定程度上彌補了OOP的短板.xml

Spring支持AspectJ的註解式編程blog

  • 使用@Aspect聲明一個切面
  • 使用@After,@Before,@Around定義加強,能夠將攔截的且做爲參數。
  • @After,@Before,@Around參數的攔截規則爲切點(PointCut),爲了使切點複用@PointCut專門定義攔截規則,而後再@After,@Before,@Around的參數種調用

例子: 註解方法 DemoAnnotationService.java @Service public class DemoAnnotationService {

@Action(value = "註解攔截的當前的add方法")
	public void add() {
	}
}

沒用註解的方法的類

@Service
public class DemoMethodService {
	public void add() {
	}
}

Action.java(註解類)

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
	String value();
}

LogAspect.java(定義切面)

@Component
@Aspect
public class LogAspect {
	//命名一個切點
	@Pointcut("@annotation(com.flexible.annotation.Action)")
	public void annotationPointcut() {
	}

	@After("annotationPointcut()")
	public void after(JoinPoint joinPoint) {
		MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
		Method method = methodSignature.getMethod();
		String name = method.getName();
		System.out.println("after註解攔截了 " + name);

	}

	@Before("execution(* com.flexible.service.DemoMethodService.*(..))")
	public void before(JoinPoint joinPoint) {
		MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
		Method method = methodSignature.getMethod();
		String name = method.getName();
		System.out.println("before註解攔截了 " + name);
	}
}

測試代碼

@Test
	public void testMethod(){
		AnnotationConfigApplicationContext configApplicationContext = new AnnotationConfigApplicationContext(AopConfig.class);
		DemoAnnotationService annotationService = configApplicationContext.getBean(DemoAnnotationService.class);
		annotationService.add();
		DemoMethodService  demoMethodService = configApplicationContext.getBean(DemoMethodService.class);
		demoMethodService.add();
	}

執行結果:

相關文章
相關標籤/搜索