Skip to content

Commit f87b1dc

Browse files
committed
Adding Chapter 10 MVC Example
1 parent e26ca33 commit f87b1dc

File tree

419 files changed

+15454
-4758
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

419 files changed

+15454
-4758
lines changed

.metadata/.log

Lines changed: 2658 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.packt.patterninspring.chapter9.bankapp.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.cache.annotation.CachePut;
5+
import org.springframework.cache.annotation.Cacheable;
6+
import org.springframework.stereotype.Service;
7+
8+
import com.packt.patterninspring.chapter9.bankapp.model.Account;
9+
import com.packt.patterninspring.chapter9.bankapp.repository.AccountRepository;
10+
11+
/**
12+
* @author Dinesh.Rajput
13+
*
14+
*/
15+
@Service
16+
public class AccountServiceImpl implements AccountService{
17+
18+
@Autowired
19+
AccountRepository accountRepository;
20+
21+
@Override
22+
@Cacheable("accountCache")
23+
public Account findOne(Long id) {
24+
return accountRepository.findAccountById(id);
25+
}
26+
27+
@Override
28+
@CachePut("accountCache")
29+
public Long save(Account account) {
30+
return accountRepository.save(account);
31+
}
32+
33+
@Override
34+
public void remove(Long id) {
35+
accountRepository.findAccountById(id);
36+
}
37+
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
*
3+
*/
4+
package com.doj.webapp.repository;
5+
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.CrudRepository;
8+
9+
import com.packt.patterninspring.chapter10.bankapp.model.Account;
10+
11+
/**
12+
* @author Dinesh.Rajput
13+
*
14+
*/
15+
public interface AccountRepository extends CrudRepository<Account, Long>{
16+
17+
Account findAccountByName(String name);
18+
19+
@Query("FROM Account where id=?1")
20+
Account readRecord(Long id);
21+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
*
3+
*/
4+
package com.packt.patterninspring.chapter10.bankapp.web.controller;
5+
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.ModelMap;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RequestMethod;
10+
11+
import com.doj.webapp.model.User;
12+
13+
/**
14+
* @author Dinesh.Rajput
15+
*
16+
*/
17+
@Controller
18+
public class HomeController {
19+
20+
@RequestMapping("/")
21+
public String home (){
22+
return "home";
23+
}
24+
25+
@RequestMapping(value = "/create", method = RequestMethod.GET)
26+
public String create (){
27+
return "addUser";
28+
}
29+
30+
@RequestMapping(value = "/create", method = RequestMethod.POST)
31+
public String saveUser (User user, ModelMap model){
32+
model.put("user", user);
33+
return "addUser";
34+
}
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.packt.patterninspring.chapter10.bankapp.service;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.cache.annotation.CacheEvict;
5+
import org.springframework.cache.annotation.CachePut;
6+
import org.springframework.cache.annotation.Cacheable;
7+
import org.springframework.stereotype.Service;
8+
9+
import com.packt.patterninspring.chapter10.bankapp.model.Account;
10+
import com.packt.patterninspring.chapter10.bankapp.repository.AccountRepository;
11+
12+
/**
13+
* @author Dinesh.Rajput
14+
*
15+
*/
16+
@Service
17+
public class AccountServiceImpl implements AccountService{
18+
19+
@Autowired
20+
AccountRepository accountRepository;
21+
22+
@Override
23+
@Cacheable("accountCache")
24+
public Account findOne(Long id) {
25+
System.out.println("findOne called");
26+
return accountRepository.findOne(id);
27+
}
28+
29+
@Override
30+
@CachePut("accountCache")
31+
public Long save(Account account) {
32+
return accountRepository.save(account);
33+
}
34+
35+
@Override
36+
@CacheEvict("accountCache")
37+
public void remove(Long id) {
38+
accountRepository.findAccountById(id);
39+
}
40+
41+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:aop="http://www.springframework.org/schema/aop"
6+
xmlns:cache="http://www.springframework.org/schema/cache"
7+
xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
8+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
9+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
10+
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
11+
12+
<!-- Enable caching -->
13+
<cache:annotation-driven />
14+
15+
<context:component-scan base-package="com.packt.patterninspring.chapter9.bankapp"/>
16+
17+
<cache:advice id="cacheAdvice">
18+
<cache:caching>
19+
<cache:cacheable cache="spittleCache" method="findRecent" />
20+
<cache:cacheable cache="spittleCache" method="findOne" />
21+
<cache:cacheable cache="spittleCache" method="findBySpitterId" />
22+
<cache:cache-put cache="spittleCache" method="save" key="#result.id" />
23+
<cache:cache-evict cache="spittleCache" method="remove" />
24+
</cache:caching>
25+
</cache:advice>
26+
27+
<!-- Declare a cache manager -->
28+
<bean id="cacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager" />
29+
</beans>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:aop="http://www.springframework.org/schema/aop"
6+
xmlns:cache="http://www.springframework.org/schema/cache"
7+
xsi:schemaLocation="http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.3.xsd
8+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
9+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
10+
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
11+
12+
<aop:config>
13+
<aop:advisor advice-ref="cacheAdvice" pointcut="execution(* com.packt.patterninspring.chapter9.bankapp.service.*(..))"/>
14+
</aop:config>
15+
16+
<cache:advice id="cacheAdvice">
17+
<cache:caching>
18+
<cache:cacheable cache="accountCache" method="findRecent" />
19+
<cache:cacheable cache="accountCache" method="findOne" />
20+
<cache:cacheable cache="accountCache" method="findBySpitterId" />
21+
<cache:cache-put cache="accountCache" method="save" key="#result.id" />
22+
<cache:cache-evict cache="accountCache" method="remove" />
23+
</cache:caching>
24+
</cache:advice>
25+
26+
<!-- Declare a cache manager -->
27+
<bean id="cacheManager" class="org.springframework.cache.concurrent.ConcurrentMapCacheManager" />
28+
</beans>

.metadata/.plugins/org.eclipse.core.resources/.history/1c/90556a6c3b7400171301d5b4e069d209

Lines changed: 0 additions & 37 deletions
This file was deleted.

.metadata/.plugins/org.eclipse.core.resources/.history/2/3027a412117a001717f0d244f0157230

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
*
3+
*/
4+
package com.doj.webapp.service;
5+
6+
import java.util.List;
7+
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.stereotype.Service;
10+
import org.springframework.transaction.annotation.Transactional;
11+
12+
import com.doj.webapp.repository.TransferRepository;
13+
import com.packt.patterninspring.chapter10.bankapp.model.Account;
14+
import com.packt.patterninspring.chapter10.bankapp.repository.AccountRepository;
15+
16+
/**
17+
* @author Dinesh.Rajput
18+
*
19+
*/
20+
@Service
21+
public class TransferServiceImpl implements TransferService{
22+
//1. always resolve dependency by type if failure then its try with by property name
23+
@Autowired
24+
AccountRepository accountRepository;
25+
26+
TransferRepository transferRepository;
27+
//As of Spring 4.3 if any class has single argument constructor then no need too define dependency definition to container
28+
//autowired always resolve dependency by type
29+
/*@Autowired
30+
public TransferServiceImpl(@Qualifier("jpaAccountRepository") AccountRepository accountRepository) {
31+
super();
32+
this.accountRepository = accountRepository;
33+
}*/
34+
35+
@Override
36+
@Transactional //Demarcation
37+
public void transfer(Long amount, Long a, Long b){
38+
Account accountA = accountRepository.findOne(a);//update 1-OK//rollback//connect
39+
Account accountB = accountRepository.findOne(b);////update 2//rollback//connect//close
40+
//transferRepository.tranfer(amount, accountB); ////update 3-Not OK-
41+
Account account = new Account(4000l);
42+
account.setBalance(5000l);
43+
account.setName("Sri");
44+
accountRepository.save(account);
45+
System.out.println(accountRepository.findAccountByName("Dinesh"));
46+
System.out.println(accountRepository.findAll());
47+
System.out.println("Amount has been transfered from "+accountA +" to "+accountB);
48+
System.out.println(accountRepository.readRecord(2000l));
49+
}
50+
51+
@Override
52+
public Account open(Account account) {
53+
return account = accountRepository.save(account);
54+
}
55+
56+
@Override
57+
public List<Account> findAllAccounts() {
58+
return (List<Account>) accountRepository.findAll();
59+
}
60+
}

0 commit comments

Comments
 (0)