Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch06] 6.6 Use annotation to regiter bean with Reflections library.
Browse files Browse the repository at this point in the history
Signed-off-by: Dongho Sim <dhsim86@gmail.com>
  • Loading branch information
dhsim86 committed Apr 15, 2017
1 parent 00751f1 commit f1c8738
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 8 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.reflections/reflections -->
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.10</version>
</dependency>

</dependencies>

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/Lesson05/MySqlMemberDao.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package Lesson05;

import javax.sql.DataSource;

import Lesson06.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
* Created by Dongho on 2017. 3. 11..
*/
@Component("memberDao")
public class MySqlMemberDao implements MemberDao {

DataSource dataSource;
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/Lesson06/ApplicationContext.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package Lesson06;

import java.io.FileReader;
import java.lang.reflect.Method;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Pattern;

import javax.naming.Context;
import javax.naming.InitialContext;

import org.reflections.Reflections;

public class ApplicationContext {

Hashtable<String, Object> objTable = new Hashtable<>();
Expand All @@ -21,6 +25,7 @@ public ApplicationContext(String propertiesPath) throws Exception {
props.load(new FileReader(propertiesPath));

prepareObjects(props);
prepareAnnotationObjects();
injectDependency();
}

Expand Down Expand Up @@ -52,6 +57,19 @@ else if (key.startsWith(">")) {
}
}

private void prepareAnnotationObjects() throws Exception {

Reflections reflector = new Reflections();

Set<Class<?>> list = reflector.getTypesAnnotatedWith(Component.class);
String key = null;

for (Class<?> clazz : list) {
key = clazz.getAnnotation(Component.class).value();
objTable.put(key, clazz.newInstance());
}
}

private void injectDependency() throws Exception {

for (String key : objTable.keySet()) {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/Lesson06/Component.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package Lesson06;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Component {
String value() default "";
}
1 change: 1 addition & 0 deletions src/main/java/Lesson06/MemberAddController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Lesson05.Member;
import Lesson05.MemberDao;

@Component("/member/add.do")
public class MemberAddController implements Controller, DataBinding {

MemberDao memberDao;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Lesson06/MemberDeleteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import Lesson05.MemberDao;

@Component("/member/delete.do")
public class MemberDeleteController implements Controller, DataBinding {

MemberDao memberDao;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Lesson06/MemberListController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import Lesson05.MemberDao;

@Component("/member/list.do")
public class MemberListController implements Controller {

MemberDao memberDao;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Lesson06/MemberLoginController.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import Lesson05.Member;
import Lesson05.MemberDao;

@Component("/auth/login.do")
public class MemberLoginController implements Controller, DataBinding {

MemberDao memberDao;
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Lesson06/MemberLogoutController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.servlet.http.HttpSession;

@Component("/auth/logout.do")
public class MemberLogoutController implements Controller, DataBinding {

@Override
Expand Down
1 change: 1 addition & 0 deletions src/main/java/Lesson06/MemberUpdateController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import Lesson05.Member;
import Lesson05.MemberDao;

@Component("/member/update.do")
public class MemberUpdateController implements Controller, DataBinding {

MemberDao memberDao;
Expand Down
9 changes: 1 addition & 8 deletions src/main/webapp/WEB-INF/application-context.properties
Original file line number Diff line number Diff line change
@@ -1,8 +1 @@
dataSource=org.apache.commons.dbcp.BasicDataSource
memberDao=Lesson05.MySqlMemberDao
/auth/login.do=Lesson06.MemberLoginController
/auth/logout.do=Lesson06.MemberLogoutController
/member/list.do=Lesson06.MemberListController
/member/add.do=Lesson06.MemberAddController
/member/update.do=Lesson06.MemberUpdateController
/member/delete.do=Lesson06.MemberDeleteController
dataSource=org.apache.commons.dbcp.BasicDataSource

0 comments on commit f1c8738

Please sign in to comment.