mirror of
https://gitee.com/many2many/java-web.git
synced 2025-01-11 14:40:55 +08:00
example changed
This commit is contained in:
parent
d7d503dc8e
commit
9fb8b80bc6
@ -0,0 +1,36 @@
|
||||
package com.lk.demo;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class FirstBean {
|
||||
|
||||
// 通过属性(字段)注入,需要使用@Autowired 注解注入
|
||||
@Autowired
|
||||
private SecondBean secondBean;
|
||||
|
||||
public FirstBean() {
|
||||
System.out.println("FirstBean created.");
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return "Hello FirstBean";
|
||||
}
|
||||
|
||||
public void doSomething() {
|
||||
System.out.println("Doing something in FirstBean.");
|
||||
secondBean.doSomething();
|
||||
}
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
System.out.println("FirstBean initialized.");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
System.out.println("FirstBean destroyed.");
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package com.lk.demo;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class InjectedBean {
|
||||
|
||||
public void print() {
|
||||
System.out.println("hello bean");
|
||||
}
|
||||
|
||||
public InjectedBean() {
|
||||
System.out.println("InjectedBean created.");
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
System.out.println("InjectedBean initialized.");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
System.out.println("InjectedBean destroyed.");
|
||||
}
|
||||
|
||||
}
|
@ -23,7 +23,13 @@ public class LoggingAspect {
|
||||
@Before("execution(* com.lk.demo.*.*(..))") // 切点定义
|
||||
public void logBefore(JoinPoint joinPoint) {
|
||||
// 连接点:在方法调用之前
|
||||
System.out.println("Executing: " + joinPoint.getSignature());
|
||||
System.out.println("--这是在调用:[" + joinPoint.getSignature()+"]之前");
|
||||
|
||||
// 获取方法参数
|
||||
Object[] args = joinPoint.getArgs();
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
System.out.println("参数: "+args[i].toString() );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -37,14 +43,18 @@ public class LoggingAspect {
|
||||
@Around("execution(* com.lk.demo.MyService.*(..))") // 切点定义
|
||||
public Object logAround(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
// 连接点:在方法调用之前
|
||||
System.out.println("Before method call...");
|
||||
System.out.println("**在调用["+ joinPoint.getSignature()+"]之前.");
|
||||
|
||||
try {
|
||||
// Proceed to the actual method invocation
|
||||
// 执行连接点上的方法
|
||||
Object result = joinPoint.proceed();
|
||||
// 连接点:在方法调用之后
|
||||
System.out.println("After method call...");
|
||||
if(result!=null){
|
||||
System.out.println("**在调用["+ joinPoint.getSignature()+"]后得到返回值:"+result.toString());
|
||||
}else{
|
||||
System.out.println("**在调用["+ joinPoint.getSignature()+"]之后.");
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
|
@ -5,16 +5,13 @@ import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import javax.swing.*;
|
||||
|
||||
@SpringBootApplication
|
||||
|
||||
// CommandLineRunner 是 Spring Boot 提供的一个特定接口,允许你在应用程序上下文加载完成后运行一些代码。
|
||||
public class Main implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private MyService myService;
|
||||
@Autowired
|
||||
private MyBean myBean;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Main.class, args);
|
||||
@ -24,8 +21,6 @@ public class Main implements CommandLineRunner {
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
myService.doSomething();
|
||||
System.out.println(myBean.getMessage());
|
||||
|
||||
|
||||
myService.workHard("hard");
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package com.lk.demo;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class MyBean {
|
||||
public String getMessage() {
|
||||
return "Hello World";
|
||||
}
|
||||
|
||||
public MyBean() {
|
||||
System.out.println("MyBean created.");
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
System.out.println("MyBean initialized.");
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
public void destroy() {
|
||||
System.out.println("MyBean destroyed.");
|
||||
}
|
||||
}
|
@ -1,17 +1,29 @@
|
||||
package com.lk.demo;
|
||||
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class MyService {
|
||||
|
||||
@Autowired
|
||||
InjectedBean injectedBean;
|
||||
private FirstBean firstBean;
|
||||
private SecondBean secondBean;
|
||||
|
||||
// 使用构造函数注入依赖
|
||||
public MyService(FirstBean firstBean, SecondBean secondBean){
|
||||
this.firstBean = firstBean;
|
||||
this.secondBean = secondBean;
|
||||
}
|
||||
|
||||
public void doSomething(){
|
||||
System.out.println("Doing something");
|
||||
injectedBean.print();
|
||||
System.out.println("Doing something in MyService.");
|
||||
firstBean.doSomething();
|
||||
secondBean.doAnother();
|
||||
}
|
||||
|
||||
public String workHard(String input){
|
||||
System.out.println("Working hard with input: " + input);
|
||||
return "Result: OK";
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
|
@ -0,0 +1,15 @@
|
||||
package com.lk.demo;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SecondBean {
|
||||
public void doSomething(){
|
||||
System.out.println("Second Bean do somting.");
|
||||
}
|
||||
|
||||
public void doAnother(){
|
||||
System.out.println("Second Bean do another.");
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user