Giter Site home page Giter Site logo

demojpaspring's Introduction

demoJpaSpring

Exemple de projet basé sur JPA + Spring + Slf4j.

Utilisation logback + slf4j

Ajouter les dépendances slf4j-api et logback-classic.

Fichier de configuration logback dans : src/main/resources/logback.xml

Déclaration d'un logger:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
@Service
public class UserService {
	Logger logger = LoggerFactory.getLogger(UserService.class);

Exemple d'utilisation dans la classe UserService :

public User updateUser(User user) {
	logger.info("Appel de updateUser");
	
	// Exemple d'utilisation de placeholders avec slf4j
	logger.debug("Modification du client {}, {}", user.getLastName(), user.getFirstName());

Liaisons Spring <-> JPA

En fait dans le web.xml, il faut ajouter le Listener suivant : <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/applicationContext-jpa.xml</param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>

Cela va entre autres charger le EntityManagerFactory (et donc lancer l'initialisation de la base). Cela devrait donc également démarrer quartz (s'il est bien configuré dans le applicationContext, je ne l'ai pas mis dans la démo).

Ensuite, au niveau de ton contrôleur ou de ta servlet (en fait à partir du moment où tu peux récupérer le ServletContext), tu récupère une référence au contexte :

ApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());

Puis tu récupères ton service, comme d'hab: UserService service = context.getBean(UserService.class);

Dans l'exemple je l'ai mis dans la JSP, mais c'est un peu crado...

Par contre, cela veut dire qu'il faut déclarer tes services dans le applicationContext et y injecter les dao. Par exemple dans mon UserService, j'ai un @Autowired (qui fait que Spring y injecte le premier bean de type UserDAO):

@Service
public class UserService {

    @Autowired
    UserDAO dao;

...
}

Donc pour résumer, lorsque ton serveur d'appli reçoit une requête HTTP, ça se fait dans l'ordre suivant : Servlet/JSP -> [ Service -> DAO -> Objet du domaine (ou entité) ] Ce qui est entre crochets est géré par Spring. Dans la démo ça fait : index.jsp -> [ UserService -> UserDAO -> User ]

demojpaspring's People

Contributors

geraudster avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.