Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review 150511 #44

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<!-- hamcrest -->
<!-- 참고: http://goo.gl/2LUY14 -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<!-- guava -->
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
27 changes: 26 additions & 1 deletion resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@
location="classpath:local-properties/database.properties,
classpath:local-properties/mailSender.properties,
classpath:local-properties/mailMessage.properties" />

<!--
1. component-sacn
<context:component-scan base-package="candrun.service, candrun.dao" />
or
<context:component-scan base-package="candrun" />

2. jdbcTemplate bean 등록
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

이렇게 설정시
아래 service, dao의 개별적 bean 등록을 안해도 됩니다.
-->




<!-- 프로퍼티 파일을 프러퍼티 객체화 -->
<util:properties id="javaMailPropertiesSmtp"
Expand All @@ -38,7 +56,14 @@
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver"
p:maxUploadSize="1000000" />


<!--
직접 bean을 등록하는 방식으로 개발시 프로젝트가 커질수록 부담도 늘어날거에요.
component-scan을 사용하길 바랍니다.
그리고 DI받는 bean의 경우 필드에 @Autowired를 사용하면 됩니다.

- 참고: http://joke00.tistory.com/162
-->
<!-- service related -->
<bean id="mailService" class="candrun.mail.CertifMailService"
p:mailContext-ref="mailContext" />
Expand Down
17 changes: 13 additions & 4 deletions src/candrun/controller/GoalsController.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,18 @@ public class GoalsController {
private static final Logger LOGGER = LoggerFactory
.getLogger(TasksController.class);
private static final int maxTasksNumber = 5;

/*
* 특별한 이유가 없는한 private을 붙여주는게 좋습니다.
*/
@Autowired
GoalDAO goalDao;
private GoalDAO goalDao;
@Autowired
TaskDAO taskDao;
private TaskDAO taskDao;
@Autowired
GoalService goalService;
private GoalService goalService;
@Autowired
TaskService taskService;
private TaskService taskService;

@RequestMapping(method = RequestMethod.POST)
public Object create(@RequestParam("goal_contents") String goalContents,
Expand All @@ -44,6 +48,11 @@ public Object create(@RequestParam("goal_contents") String goalContents,
ArrayList<String> tasks = new ArrayList<String>();

for (int i = 0; i < maxTasksNumber; i++) {
/*
* 클라이언트에서 값을 전달할 때부터 배열이면 좋을 것 같아요.
* - 클라참고: http://goo.gl/BORzx6
* - 서버참고: http://goo.gl/rCZ3Jq
*/
String taskContents = req.getParameter("task_contents_" + i);
if (taskContents == null) break;
tasks.add(taskContents);
Expand Down
31 changes: 31 additions & 0 deletions src/candrun/dao/TaskDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@

import candrun.model.Task;

/*
* applicationContext.xml에서 component-scan을 걸어두었다면
* @Repository, @Service, @Component 등의 어노테이션을 바탕으로 bean으로 읽게됩니다.
*
* 보통 JdbcTemplate을 사용하는 Dao의 경우
* 1. JdbcTemplate 내부에 dataSource의 참조를 가지고 있으므로 Dao에 직접 set할 필요는 없습니다.
* 2. JdbcTemplate은 의미대로 템플릿이므로 하나의 객체만 있으면 충분합니다.
* 따라서 field에 참조를 두더라도 thread-safe 합니다.
*
* 아래는 위 내용으로 재구성한 dao의 모습입니다.
* (반드시 해당 패키지를 component-scan 해야만 합니다)
*

@Repository
public class TaskDAO {

@Autowired
private JdbcTemplate jdbcTemplate;

public void addTask(Task task) {
String sql = "INSERT INTO tasks(contents, goal_id) VALUES(?, ?)";
this.jdbcTemplate.update(sql, task.getContents(), task.getGoalId());
}

// 같은 goal아래 tasks을 불러온다
public List<Task> getTasksByGoalId(int goalId) {
String sql = "SELECT * FROM tasks WHERE goal_id = ? LIMIT 3";
return this.jdbcTemplate.query(sql, new TaskMapper(), goalId);
}
*/

public class TaskDAO extends JdbcDaoSupport {

private static final class TaskMapper implements RowMapper<Task> {
Expand Down
31 changes: 31 additions & 0 deletions src/candrun/model/User.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package candrun.model;




public class User {
private String email;
private String nickname;
private String password;
private String picPath;
private int state;

public User() { }

public User(String email, String nickname, String password, String picPath) {
this.email = email;
this.nickname = nickname;
Expand Down Expand Up @@ -38,6 +43,32 @@ public int getState() {
return state;
}

/*
* @see candrun.model.User
* @see candrun.model.UserTest
* @see candrun.service.UserService
*
* @see candrun.support.enums.Value
* @see candrun.support.enums.UserErrorcode
* @see candrun.support.enums.CommonInvar
*

이때 주의할 점은 같은 타입으로 반환해야하는데요.
제 생각엔 enum에 value타입을 지정하여 통일시켜주는게 좋을것 같아요.

public Value getEnumState() {

switch (state) {
case 0 :
return UserErrorcode.NOT_YET_CERTI;
case 1 :
return CommonInvar.SUCCESS;
default :
return CommonInvar.DEFAULT;
}
}
*/

public String getPicPath() {
return picPath;
}
Expand Down
5 changes: 5 additions & 0 deletions src/candrun/scheduled/DailyInitializeJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@

import candrun.dao.TaskDAO;

/*
* 스프링3 @Scheduled 어노테이션으로 job을 지원하고 있습니다 ㅠㅠ
* - 참고: http://jsonobject.tistory.com/101
* - 주의사항: xml에 task 설정시 task namespace 꼭 확인 바랍니다.
*/
public class DailyInitializeJob extends QuartzJobBean{

TaskDAO taskDao;
Expand Down
28 changes: 28 additions & 0 deletions src/candrun/service/GoalService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,34 @@
import candrun.support.enums.GoalState;
import candrun.support.enums.RelationRequestState;

/*
* applicationContext에서 component-sacn을 사용할 때 예제입니다
*

@Service
public class GoalService {
private static final Logger logger = LoggerFactory
.getLogger(GoalService.class);

@Autowired
private GoalDAO goalDao;

@Autowired
private UserDAO userDao;

@Autowired
private TaskDAO taskDao;

// 개별적인 DI를 수행하므로 아래 생성자는 필요 없습니다.
public GoalService(GoalDAO goalDao, UserDAO userDao, TaskDAO taskDao) {
this.goalDao = goalDao;
this.userDao = userDao;
this.taskDao = taskDao;
}

public List<GoalRelation> getGoalRelations(String email) { ...
*/

public class GoalService {
private static final Logger logger = LoggerFactory
.getLogger(GoalService.class);
Expand Down
22 changes: 22 additions & 0 deletions src/candrun/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,28 @@ public String login(String email, String pw, HttpSession session)
return UserErrorcode.WRONG_PW.getValue();
}

/*
* @see candrun.model.User
* @see candrun.model.UserTest
* @see candrun.service.UserService
*
* @see candrun.support.enums.Value
* @see candrun.support.enums.UserErrorcode
* @see candrun.support.enums.CommonInvar

객체지향의 특징은 문제를 요청하고 결과를 받는다는 것입니다.
보통은 이것을 객체간 메시지를 주고 받는다고 하더라구요.

지금의 코드는 다음과 같이 변경하면 좋을 것 같아요.
이러한 코드의 장점은 핵심 로직을 분리하여 유지보수가 쉽고, 가독성도 높아지고, 테스트도 좋아집니다.

userState = user.getEnumState();
if (CommonInvar.SUCCESS.equals(userState) {
session.setAttribute("email", email);
}
return userState.getValue();

*/
userState = user.getState();
if (userState == 0) {
return UserErrorcode.NOT_YET_CERTI.getValue();
Expand Down
17 changes: 17 additions & 0 deletions src/candrun/service/aspect/SecurityAspect.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@

import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -24,6 +26,21 @@ public class SecurityAspect {
+ "|| execution(public * candrun.controller.AuthController.signIn(..))")
public void decrypUserInfo() {
};

/*
* 돌려보지는 않았지만 before만 쓴다면 이게 조금 더 명확할 것 같아요!!
*
@Before("decrypUserInfo()")
public void beforeRegister(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs();
logger.debug("before start method");
PrivateKey privateKey = (PrivateKey) ((HttpSession) args[0])
.getAttribute(Security.RSA_PRI_KEY.getValue());
args[1] = SecurityService.decrytRsa(privateKey, (String) args[1]);
args[2] = SecurityService.decrytRsa(privateKey, (String) args[2]);
logger.debug("email: " + (String) args[1] + "/" + "password: " + (String) args[2]);
}
*/

@SuppressWarnings("unchecked")
@Around("decrypUserInfo()")
Expand Down
12 changes: 12 additions & 0 deletions src/candrun/support/enums/CommonInvar.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package candrun.support.enums;

/*
* @see candrun.model.User
* @see candrun.model.UserTest
* @see candrun.service.UserService
*
* @see candrun.support.enums.Value
* @see candrun.support.enums.UserErrorcode
*
* enum interfcae를 구성하여 지정하면 Vaule타입으로 통합될 수 있습니다
*/

//public enum CommonInvar implements Value {
public enum CommonInvar {
ERRORCODE ("errorCode"),
RETURNMSG ("returnMsg"),
Expand Down
12 changes: 12 additions & 0 deletions src/candrun/support/enums/UserErrorcode.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
package candrun.support.enums;

/*
* @see candrun.model.User
* @see candrun.model.UserTest
* @see candrun.service.UserService
*
* @see candrun.support.enums.Value
* @see candrun.support.enums.CommonInvar
*
* enum interfcae를 구성하여 지정하면 Vaule타입으로 통합될 수 있습니
*/

//public enum UserErrorcode implements Value {
public enum UserErrorcode {
EMPTY ("empty"),
DUP ("dup"),
Expand Down
17 changes: 17 additions & 0 deletions src/candrun/support/enums/Value.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package candrun.support.enums;


/*
* @see candrun.model.User
* @see candrun.model.UserTest
* @see candrun.service.UserService
*
* @see candrun.support.enums.Value
* @see candrun.support.enums.UserErrorcode
* @see candrun.support.enums.CommonInvar
*/
public interface Value {

String getValue();

}
49 changes: 49 additions & 0 deletions test/candrun/model/UserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package candrun.model;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.lang.reflect.Field;

import org.junit.Test;

import candrun.support.enums.CommonInvar;
import candrun.support.enums.UserErrorcode;

public class UserTest {
/*
@Test
public void getEnumState_S_state가_0일때() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
// defualt 생성자 필요
User user = new User();
Field field = User.class.getDeclaredField("state");
field.setAccessible(true);
field.set(user, 0);

assertThat(user.getEnumState(), equalTo(UserErrorcode.NOT_YET_CERTI));
}

@Test
public void getEnumState_S_state가_1일때() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
// defualt 생성자 필요
User user = new User();
Field field = User.class.getDeclaredField("state");
field.setAccessible(true);
field.set(user, 1);

assertThat(user.getEnumState(), equalTo(CommonInvar.SUCCESS));
}

@Test
public void getEnumState_S_state가_0_1이_아닐때() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
// defualt 생성자 필요
User user = new User();
Field field = User.class.getDeclaredField("state");
field.setAccessible(true);
field.set(user, 1000);

assertThat(user.getEnumState(), equalTo(CommonInvar.DEFAULT));
}
*/

}