Merge pull request #802 from nguyenminhtuanfit/master
aspectj introduction
This commit is contained in:
commit
62bcec6d87
131
aspectj/pom.xml
Normal file
131
aspectj/pom.xml
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
<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>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>aspectj</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>aspectj</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjrt</artifactId>
|
||||||
|
<version>${aspectj.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.aspectj</groupId>
|
||||||
|
<artifactId>aspectjweaver</artifactId>
|
||||||
|
<version>${aspectj.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- utils -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-classic</artifactId>
|
||||||
|
<version>${logback.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-core</artifactId>
|
||||||
|
<version>${logback.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- unit test -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>aspectj</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${source.version}</source>
|
||||||
|
<target>${source.version}</target>
|
||||||
|
</configuration>
|
||||||
|
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>aspectj-maven-plugin</artifactId>
|
||||||
|
<version>1.7</version>
|
||||||
|
<configuration>
|
||||||
|
<complianceLevel>${source.version}</complianceLevel>
|
||||||
|
<source>${source.version}</source>
|
||||||
|
<target>${source.version}</target>
|
||||||
|
<showWeaveInfo>true</showWeaveInfo>
|
||||||
|
<verbose>true</verbose>
|
||||||
|
<Xlint>ignore</Xlint>
|
||||||
|
<encoding>${project.build.sourceEncoding}</encoding>
|
||||||
|
<!-- Post-compile weaving -->
|
||||||
|
<!--
|
||||||
|
<weaveDependencies>
|
||||||
|
<weaveDependency>
|
||||||
|
<groupId>org.agroup</groupId>
|
||||||
|
<artifactId>to-weave</artifactId>
|
||||||
|
</weaveDependency>
|
||||||
|
<weaveDependency>
|
||||||
|
<groupId>org.anothergroup</groupId>
|
||||||
|
<artifactId>gen</artifactId>
|
||||||
|
</weaveDependency>
|
||||||
|
</weaveDependencies>
|
||||||
|
-->
|
||||||
|
</configuration>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
<goal>test-compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<!-- <plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
<version>2.10</version>
|
||||||
|
<configuration>
|
||||||
|
<argLine>-javaagent:"${settings.localRepository}"/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar</argLine>
|
||||||
|
<useSystemClassLoader>true</useSystemClassLoader>
|
||||||
|
<forkMode>always</forkMode>
|
||||||
|
</configuration>
|
||||||
|
</plugin> -->
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<source.version>1.8</source.version>
|
||||||
|
<aspectj.version>1.6.11</aspectj.version>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<aspectj.version>1.8.9</aspectj.version>
|
||||||
|
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||||
|
<logback.version>1.1.7</logback.version>
|
||||||
|
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
13
aspectj/src/main/java/com/baeldung/aspectj/Account.java
Normal file
13
aspectj/src/main/java/com/baeldung/aspectj/Account.java
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package com.baeldung.aspectj;
|
||||||
|
|
||||||
|
public class Account {
|
||||||
|
int balance = 20;
|
||||||
|
|
||||||
|
public boolean withdraw(int amount) {
|
||||||
|
if (balance - amount > 0) {
|
||||||
|
balance = balance - amount;
|
||||||
|
return true;
|
||||||
|
} else
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
30
aspectj/src/main/java/com/baeldung/aspectj/AccountAspect.aj
Normal file
30
aspectj/src/main/java/com/baeldung/aspectj/AccountAspect.aj
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package com.baeldung.aspectj;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public aspect AccountAspect {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(AccountAspect.class);
|
||||||
|
final int MIN_BALANCE = 10;
|
||||||
|
|
||||||
|
pointcut callWithDraw(int amount, Account account):
|
||||||
|
call(boolean Account.withdraw(int)) && args(amount) && target(account);
|
||||||
|
|
||||||
|
before(int amount, Account account) : callWithDraw(amount, account) {
|
||||||
|
logger.info(" Balance before withdrawal: {}", account.balance);
|
||||||
|
logger.info(" Withdraw ammout: {}", amount);
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean around(int amount, Account account) : callWithDraw(amount, account) {
|
||||||
|
if (account.balance - amount >= MIN_BALANCE)
|
||||||
|
return proceed(amount, account);
|
||||||
|
else {
|
||||||
|
logger.info("Withdrawal Rejected!");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
after(int amount, Account balance) : callWithDraw(amount, balance) {
|
||||||
|
logger.info("Balance after withdrawal : {}", balance.balance);
|
||||||
|
}
|
||||||
|
}
|
12
aspectj/src/main/java/com/baeldung/aspectj/Secured.java
Normal file
12
aspectj/src/main/java/com/baeldung/aspectj/Secured.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.aspectj;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
public @interface Secured {
|
||||||
|
public boolean isLocked() default false;
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.aspectj;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public class SecuredMethod {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(SecuredMethod.class);
|
||||||
|
|
||||||
|
@Secured(isLocked = true)
|
||||||
|
public void lockedMethod() throws Exception {
|
||||||
|
logger.info("lockedMethod");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Secured(isLocked = false)
|
||||||
|
public void unlockedMethod() {
|
||||||
|
logger.info("unlockedMethod");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
SecuredMethod sv = new SecuredMethod();
|
||||||
|
sv.lockedMethod();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.aspectj;
|
||||||
|
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.aspectj.lang.annotation.Pointcut;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
public class SecuredMethodAspect {
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(SecuredMethodAspect.class);
|
||||||
|
|
||||||
|
@Pointcut("@annotation(secured)")
|
||||||
|
public void callAt(Secured secured) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Around("callAt(secured)")
|
||||||
|
public Object around(ProceedingJoinPoint pjp, Secured secured) throws Throwable {
|
||||||
|
if (secured.isLocked()) {
|
||||||
|
logger.info(pjp.getSignature().toLongString() + " is locked");
|
||||||
|
return null;
|
||||||
|
} else {
|
||||||
|
return pjp.proceed();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
aspectj/src/main/resources/META-INF/aop.xml
Normal file
8
aspectj/src/main/resources/META-INF/aop.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<aspectj>
|
||||||
|
<aspects>
|
||||||
|
<aspect name="com.baeldung.aspectj.SecuredMethodAspect"/>
|
||||||
|
<weaver options="-verbose -showWeaveInfo">
|
||||||
|
<include within="com.baeldung.aspectj.*"/>
|
||||||
|
</weaver>
|
||||||
|
</aspects>
|
||||||
|
</aspectj>
|
18
aspectj/src/main/resources/logback.xml
Normal file
18
aspectj/src/main/resources/logback.xml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||||
|
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg %n</pattern>
|
||||||
|
</layout>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="com.baeldung.hazelcast" level="INFO" additivity="false">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</logger>
|
||||||
|
|
||||||
|
<root level="DEBUG">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
|
||||||
|
</configuration>
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.baeldung.aspectj.test;
|
||||||
|
|
||||||
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.aspectj.Account;
|
||||||
|
|
||||||
|
public class AccountTest {
|
||||||
|
private Account account;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void before() {
|
||||||
|
account = new Account();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBalance20AndMinBalance10_whenWithdraw5_thenSuccess() {
|
||||||
|
assertTrue(account.withdraw(5));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenBalance20AndMinBalance10_whenWithdraw100_thenFail() {
|
||||||
|
assertFalse(account.withdraw(100));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.aspectj.test;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import com.baeldung.aspectj.SecuredMethod;
|
||||||
|
|
||||||
|
public class SecuredMethodTest {
|
||||||
|
@Test
|
||||||
|
public void testMethod() throws Exception {
|
||||||
|
SecuredMethod service = new SecuredMethod();
|
||||||
|
service.unlockedMethod();
|
||||||
|
service.lockedMethod();
|
||||||
|
}
|
||||||
|
}
|
170
jaxb/pom.xml
Normal file
170
jaxb/pom.xml
Normal file
@ -0,0 +1,170 @@
|
|||||||
|
<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>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>jaxb</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<name>jaxb</name>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.jaxb</groupId>
|
||||||
|
<artifactId>jaxb-runtime</artifactId>
|
||||||
|
<version>${jaxb.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.glassfish.jaxb</groupId>
|
||||||
|
<artifactId>jaxb-core</artifactId>
|
||||||
|
<version>${jaxb.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<!-- utils -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.sun.istack</groupId>
|
||||||
|
<artifactId>istack-commons-runtime</artifactId>
|
||||||
|
<version>3.0.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.slf4j</groupId>
|
||||||
|
<artifactId>slf4j-api</artifactId>
|
||||||
|
<version>${org.slf4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-classic</artifactId>
|
||||||
|
<version>${logback.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>ch.qos.logback</groupId>
|
||||||
|
<artifactId>logback-core</artifactId>
|
||||||
|
<version>${logback.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<finalName>jaxb</finalName>
|
||||||
|
<resources>
|
||||||
|
<resource>
|
||||||
|
<directory>src/main/resources</directory>
|
||||||
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
|
||||||
|
<pluginManagement>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.eclipse.m2e</groupId>
|
||||||
|
<artifactId>lifecycle-mapping</artifactId>
|
||||||
|
<version>1.0.0</version>
|
||||||
|
<configuration>
|
||||||
|
<lifecycleMappingMetadata>
|
||||||
|
<pluginExecutions>
|
||||||
|
<pluginExecution>
|
||||||
|
<pluginExecutionFilter>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>jaxb2-maven-plugin</artifactId>
|
||||||
|
<versionRange>[${jaxb2-maven-plugin.version},)</versionRange>
|
||||||
|
<goals>
|
||||||
|
<goal>schemagen</goal>
|
||||||
|
<goal>xjc</goal>
|
||||||
|
</goals>
|
||||||
|
</pluginExecutionFilter>
|
||||||
|
<action>
|
||||||
|
<execute />
|
||||||
|
</action>
|
||||||
|
</pluginExecution>
|
||||||
|
</pluginExecutions>
|
||||||
|
</lifecycleMappingMetadata>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</pluginManagement>
|
||||||
|
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven-compiler-plugin.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<!-- xjc -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>jaxb2-maven-plugin</artifactId>
|
||||||
|
<version>${jaxb2-maven-plugin.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>xjc</id>
|
||||||
|
<goals>
|
||||||
|
<goal>xjc</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<xjbSources>
|
||||||
|
<xjbSource>src/main/resources/global.xjb</xjbSource>
|
||||||
|
</xjbSources>
|
||||||
|
<sources>
|
||||||
|
<source>src/main/resources/user.xsd</source>
|
||||||
|
</sources>
|
||||||
|
<outputDirectory>${basedir}/src/main/java</outputDirectory>
|
||||||
|
<clearOutputDir>false</clearOutputDir>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
|
||||||
|
<!-- schemagen -->
|
||||||
|
<!--
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.codehaus.mojo</groupId>
|
||||||
|
<artifactId>jaxb2-maven-plugin</artifactId>
|
||||||
|
<version>${jaxb2-maven-plugin.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>schemagen</id>
|
||||||
|
<goals>
|
||||||
|
<goal>schemagen</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<configuration>
|
||||||
|
<sources>
|
||||||
|
<source>src/main/java/com/baeldung/jaxb/gen</source>
|
||||||
|
</sources>
|
||||||
|
<outputDirectory>src/main/resources</outputDirectory>
|
||||||
|
<clearOutputDir>false</clearOutputDir>
|
||||||
|
<transformSchemas>
|
||||||
|
<transformSchema>
|
||||||
|
<uri>http://www.baeldung.com/jaxb/gen</uri>
|
||||||
|
<toPrefix>user</toPrefix>
|
||||||
|
<toFile>gen-schema.xsd</toFile>
|
||||||
|
</transformSchema>
|
||||||
|
</transformSchemas>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
-->
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<!-- jaxb -->
|
||||||
|
<jaxb.version>2.2.11</jaxb.version>
|
||||||
|
|
||||||
|
<!-- logging -->
|
||||||
|
<org.slf4j.version>1.7.21</org.slf4j.version>
|
||||||
|
<logback.version>1.1.7</logback.version>
|
||||||
|
|
||||||
|
<!-- maven plugins -->
|
||||||
|
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
|
||||||
|
<jaxb2-maven-plugin.version>2.3</jaxb2-maven-plugin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
58
jaxb/src/main/java/com/baeldung/jaxb/Book.java
Normal file
58
jaxb/src/main/java/com/baeldung/jaxb/Book.java
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
package com.baeldung.jaxb;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.XmlAttribute;
|
||||||
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
|
import javax.xml.bind.annotation.XmlTransient;
|
||||||
|
import javax.xml.bind.annotation.XmlType;
|
||||||
|
|
||||||
|
@XmlRootElement(name = "book")
|
||||||
|
@XmlType(propOrder = { "id", "name", "date" })
|
||||||
|
public class Book {
|
||||||
|
private Long id;
|
||||||
|
private String name;
|
||||||
|
private String author;
|
||||||
|
private Date date;
|
||||||
|
|
||||||
|
@XmlAttribute
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlElement(name = "title")
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@XmlTransient
|
||||||
|
public void setAuthor(String author) {
|
||||||
|
this.author = author;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getDate() {
|
||||||
|
return date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDate(Date date) {
|
||||||
|
this.date = date;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAuthor() {
|
||||||
|
return author;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Book [id=" + id + ", name=" + name + ", author=" + author + ", date=" + date + "]";
|
||||||
|
}
|
||||||
|
}
|
28
jaxb/src/main/java/com/baeldung/jaxb/DateAdapter.java
Normal file
28
jaxb/src/main/java/com/baeldung/jaxb/DateAdapter.java
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package com.baeldung.jaxb;
|
||||||
|
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
||||||
|
|
||||||
|
public class DateAdapter extends XmlAdapter<String, Date> {
|
||||||
|
|
||||||
|
private static final ThreadLocal<DateFormat> dateFormat = new ThreadLocal<DateFormat>() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected DateFormat initialValue() {
|
||||||
|
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Date unmarshal(String v) throws Exception {
|
||||||
|
return dateFormat.get().parse(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String marshal(Date v) throws Exception {
|
||||||
|
return dateFormat.get().format(v);
|
||||||
|
}
|
||||||
|
}
|
39
jaxb/src/main/java/com/baeldung/jaxb/Main.java
Normal file
39
jaxb/src/main/java/com/baeldung/jaxb/Main.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package com.baeldung.jaxb;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
import javax.xml.bind.JAXBContext;
|
||||||
|
import javax.xml.bind.JAXBException;
|
||||||
|
import javax.xml.bind.Marshaller;
|
||||||
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void marshal() throws JAXBException, IOException {
|
||||||
|
Book book = new Book();
|
||||||
|
book.setId(1L);
|
||||||
|
book.setName("Book1");
|
||||||
|
book.setAuthor("Author1");
|
||||||
|
book.setDate(new Date());
|
||||||
|
|
||||||
|
JAXBContext context = JAXBContext.newInstance(Book.class);
|
||||||
|
Marshaller marshaller = context.createMarshaller();
|
||||||
|
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
|
||||||
|
marshaller.marshal(book, new File("./book.xml"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Book unMashal() throws JAXBException, IOException {
|
||||||
|
JAXBContext context = JAXBContext.newInstance(Book.class);
|
||||||
|
Unmarshaller unmarshaller = context.createUnmarshaller();
|
||||||
|
Book book = (Book) unmarshaller.unmarshal(new FileReader("./book.xml"));
|
||||||
|
return book;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws JAXBException, IOException {
|
||||||
|
marshal();
|
||||||
|
Book book = unMashal();
|
||||||
|
System.out.println(book.toString());
|
||||||
|
}
|
||||||
|
}
|
13
jaxb/src/main/resources/global.xjb
Normal file
13
jaxb/src/main/resources/global.xjb
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
||||||
|
<jaxb:bindings version="2.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
|
||||||
|
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xs="http://www.w3.org/2001/XMLSchema"
|
||||||
|
jaxb:extensionBindingPrefixes="xjc">
|
||||||
|
|
||||||
|
<jaxb:globalBindings>
|
||||||
|
<xjc:simple />
|
||||||
|
<xjc:serializable uid="-1" />
|
||||||
|
<jaxb:javaType name="java.util.Calendar" xmlType="xs:dateTime"
|
||||||
|
parseMethod="javax.xml.bind.DatatypeConverter.parseDateTime"
|
||||||
|
printMethod="javax.xml.bind.DatatypeConverter.printDateTime" />
|
||||||
|
</jaxb:globalBindings>
|
||||||
|
</jaxb:bindings>
|
18
jaxb/src/main/resources/logback.xml
Normal file
18
jaxb/src/main/resources/logback.xml
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<layout class="ch.qos.logback.classic.PatternLayout">
|
||||||
|
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg %n</pattern>
|
||||||
|
</layout>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<logger name="com.baeldung.hazelcast" level="INFO" additivity="false">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</logger>
|
||||||
|
|
||||||
|
<root level="DEBUG">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
|
||||||
|
</configuration>
|
23
jaxb/src/main/resources/user.xsd
Normal file
23
jaxb/src/main/resources/user.xsd
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.baeldung.com/jaxb/gen"
|
||||||
|
xmlns:userns="http://www.baeldung.com/jaxb/gen" elementFormDefault="qualified">
|
||||||
|
|
||||||
|
<element name="userRequest" type="userns:UserRequest"></element>
|
||||||
|
<element name="userResponse" type="userns:UserResponse"></element>
|
||||||
|
|
||||||
|
<complexType name="UserRequest">
|
||||||
|
<sequence>
|
||||||
|
<element name="id" type="int" />
|
||||||
|
<element name="name" type="string" />
|
||||||
|
</sequence>
|
||||||
|
</complexType>
|
||||||
|
|
||||||
|
<complexType name="UserResponse">
|
||||||
|
<sequence>
|
||||||
|
<element name="id" type="int" />
|
||||||
|
<element name="name" type="string" />
|
||||||
|
<element name="gender" type="string" />
|
||||||
|
<element name="created" type="dateTime" />
|
||||||
|
</sequence>
|
||||||
|
</complexType>
|
||||||
|
</schema>
|
Loading…
x
Reference in New Issue
Block a user