diff options
Diffstat (limited to 'spring-framework/20-aop-xml')
8 files changed, 197 insertions, 0 deletions
diff --git a/spring-framework/20-aop-xml/.gitignore b/spring-framework/20-aop-xml/.gitignore new file mode 100644 index 0000000..ec4e05e --- /dev/null +++ b/spring-framework/20-aop-xml/.gitignore @@ -0,0 +1,9 @@ +# Eclipse +bin +.settings +.metadata +.classpath +.project + +# Maven +target diff --git a/spring-framework/20-aop-xml/pom.xml b/spring-framework/20-aop-xml/pom.xml new file mode 100644 index 0000000..8b723ef --- /dev/null +++ b/spring-framework/20-aop-xml/pom.xml @@ -0,0 +1,30 @@ +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + <modelVersion>4.0.0</modelVersion> + + <parent> + <groupId>com.example.spring</groupId> + <artifactId>base-config</artifactId> + <version>0.0.1-SNAPSHOT</version> + <relativePath>../00-config</relativePath> + </parent> + + <artifactId>aop-xml</artifactId> + <packaging>jar</packaging> + + <dependencies> + <dependency> + <groupId>org.springframework</groupId> + <artifactId>spring-aop</artifactId> + <version>${springVersion}</version> + </dependency> + + <dependency> + <groupId>org.aspectj</groupId> + <artifactId>aspectjweaver</artifactId> + <version>${aspectjVersion}</version> + </dependency> + + </dependencies> +</project> diff --git a/spring-framework/20-aop-xml/src/main/java/com/example/spring/App.java b/spring-framework/20-aop-xml/src/main/java/com/example/spring/App.java new file mode 100644 index 0000000..13a782c --- /dev/null +++ b/spring-framework/20-aop-xml/src/main/java/com/example/spring/App.java @@ -0,0 +1,11 @@ +package com.example.spring; + +import com.example.spring.aop.BankApp; + +public class App { + public static void main(String[] args) { + // Actually, BankApp is the class with the main method. + // It should have been executed directly instead of this class. + BankApp.main(args); + } +} diff --git a/spring-framework/20-aop-xml/src/main/java/com/example/spring/aop/Account.java b/spring-framework/20-aop-xml/src/main/java/com/example/spring/aop/Account.java new file mode 100644 index 0000000..f2d0e84 --- /dev/null +++ b/spring-framework/20-aop-xml/src/main/java/com/example/spring/aop/Account.java @@ -0,0 +1,38 @@ +package com.example.spring.aop; + +public class Account { + private int accNo; + private double balance; + + public void withdraw(double amount) { + if (balance >= amount) { + balance -= amount; + } + } + + public void deposit(double amount) { +// System.out.println("Account.deposit()"); + balance += amount; + } + + + public void printDetails() { + System.out.println("Balance of " + accNo + " : " + balance); + } + + public int getAccNo() { + return accNo; + } + + public void setAccNo(int accNo) { + this.accNo = accNo; + } + + public double getBalance() { + return balance; + } + + public void setBalance(double balance) { + this.balance = balance; + } +} diff --git a/spring-framework/20-aop-xml/src/main/java/com/example/spring/aop/BankApp.java b/spring-framework/20-aop-xml/src/main/java/com/example/spring/aop/BankApp.java new file mode 100644 index 0000000..760b139 --- /dev/null +++ b/spring-framework/20-aop-xml/src/main/java/com/example/spring/aop/BankApp.java @@ -0,0 +1,29 @@ +package com.example.spring.aop; + +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class BankApp { + + /** + * @param args + */ + public static void main(String[] args) { + ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(new String[] { + "classpath:META-INF/spring/applicationContext.xml",}); + + Account acc = ctx.getBean("account", Account.class); + + acc.printDetails(); // 250 + acc.withdraw(10); + acc.printDetails(); // 240 + acc.deposit(20); + acc.printDetails(); // 260 + + System.out.println("An exception should occur here if attempted to withdraw a large amount."); + acc.withdraw(1200); // Uncommented, this should throw an exception + acc.printDetails(); // 260 + + ctx.close(); + } + +} diff --git a/spring-framework/20-aop-xml/src/main/java/com/example/spring/aop/security/AccessChecker.java b/spring-framework/20-aop-xml/src/main/java/com/example/spring/aop/security/AccessChecker.java new file mode 100644 index 0000000..97384c3 --- /dev/null +++ b/spring-framework/20-aop-xml/src/main/java/com/example/spring/aop/security/AccessChecker.java @@ -0,0 +1,15 @@ +package com.example.spring.aop.security; + +public class AccessChecker { + + public void checkDepositPermission() { + System.out.println("Aspect is checking deposit permission..."); + } + + public void checkWithdrawPermission(double amount) { + System.out.println("Aspect is checking withdraw permission for " + amount); + if (amount > 1000.0) { + throw new AccessException("Withdrawing more than 1000.0 is not allowed."); + } + } +} diff --git a/spring-framework/20-aop-xml/src/main/java/com/example/spring/aop/security/AccessException.java b/spring-framework/20-aop-xml/src/main/java/com/example/spring/aop/security/AccessException.java new file mode 100644 index 0000000..0e1b60c --- /dev/null +++ b/spring-framework/20-aop-xml/src/main/java/com/example/spring/aop/security/AccessException.java @@ -0,0 +1,30 @@ +package com.example.spring.aop.security; + +public class AccessException extends RuntimeException { + + + /** + * + */ + private static final long serialVersionUID = -6529989268011261235L; + + public AccessException() { + // TODO Auto-generated constructor stub + } + + public AccessException(String message) { + super(message); + // TODO Auto-generated constructor stub + } + + public AccessException(Throwable cause) { + super(cause); + // TODO Auto-generated constructor stub + } + + public AccessException(String message, Throwable cause) { + super(message, cause); + // TODO Auto-generated constructor stub + } + +} diff --git a/spring-framework/20-aop-xml/src/main/resources/META-INF/spring/applicationContext.xml b/spring-framework/20-aop-xml/src/main/resources/META-INF/spring/applicationContext.xml new file mode 100644 index 0000000..ecf08e4 --- /dev/null +++ b/spring-framework/20-aop-xml/src/main/resources/META-INF/spring/applicationContext.xml @@ -0,0 +1,35 @@ +<?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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> + + <bean id="account" class="com.example.spring.aop.Account"> + <property name="accNo" value="1000"></property> + <property name="balance" value="250.00"></property> + </bean> + + <!-- Here is a bean representing the security aspect --> + + <bean id="accessChecker" class="com.example.spring.aop.security.AccessChecker" /> + + <aop:config> + <aop:aspect id="securityAspect" ref="accessChecker"> + + <aop:pointcut id="deposit" + expression="execution(public * com.example.spring.aop.Account.deposit(..))" /> + <aop:before pointcut-ref="deposit" + method="checkDepositPermission" /> + + <aop:pointcut id="withdraw" + expression="execution(public * com.example.spring.aop.Account.withdraw(..)) and args(amount)" /> + <aop:before pointcut-ref="withdraw" + method="checkWithdrawPermission(double)" /> + + </aop:aspect> + + </aop:config> + +</beans>
\ No newline at end of file |
