- download Eclipsed Jave EE distrubition from
http://www.eclipse.org/
- download Java SE JDK
http://www.oracle.com/technetwork/java/javase/overview/index.html
- download lastest release of Spring libs from
http://repo.spring.io/release/org/springframework/spring/
- download tomcat from
http://tomcat.apache.org/
- link local tomcat server in Eclipse at
server
tab - Eclipse - Window - Perspective - Open Perspective - Java
- add Spring libraries JAR to your project
- Copy Spring /libs/xxx.jar to project /lib folder
- Project Properties - Java Build Path - Libraries - Add JARs - add all of them
- config in
applicationContext.xml
- remember import springframework JARs b/c ClassPathXmlApplicationContext is essentail here
3.Dependency Injection - Constructor Injection
- update service bean in
applicationContext.xml
- setup constructor injection in
applicationContext.xml
4.Dependency Injection - Setter Injection
- setup setter injection in
applicationContext.xml
- convention: invoke set + property name(capital case) method
5.Dependency Injection - Literal Value Injection
- setup literal value injection in
applicationContext.xml
- value attribute in property tag will be injected when invoking set + property name method
6.Literal Value Injection in Another Properties File
- make literal values maintainable in one single file
- default bean scope is singleton, i.e. application container always returns same instance. - stateless object
- set
scope="prototype"
inbeanScope-applicaitonContext.xml
file, thus different instances returned. - stateful object
- Convention:
- The methods should be public void
- The methods should be no-arg, meaning they shouldn't accept any method arguments
- Life Cycle:
- Container started
- Bean instantiated
- Dependencies Injected
- Internal Spring Processing
- Your Custom Init Method
- Bean Is Already For Use
- Container Is Shut Down
- Your Custom Destroy Method
- STOP
- Cavet:
Spring does not manage the complete lifecycle of a
prototype
bean: the container instantiates, configures, and otherwise assembles a prototype object, and hands it to the client, with no further record of that prototype instance. Thus, although initialization lifecycle callback methods are called on all objects regardless of scope, in the case of prototypes, configured destruction lifecycle callbacks are not called. The client code must clean up prototype-scoped objects and release expensive resources that the prototype bean(s) are holding. --- This also applies to both XML configuration and Annotation-based configuration.
- Spring libraries
- javax.annotation-api-1.3.1.jar
- applicationContext.xml
copy from last demo or search internet
1.Annotation - Explicit Component Name
- annotation minimizes the config than xml files
- setup
context-component-scan
inapplicationContext.xml
- @Component annotataion in business code
2.Annotation - Default Component Name
- default bean id is class name in camel case
3.Annotation - Constructor Injection
- Spring will automatically scan our package since we've set up in
applicationContext.xml
@Component
the Service class@Autowired
the constructor
4.Annotation - Setter Injection
@Autowired
a setter method inside
5.Annotation - Method Injection
- every method can be annotated by
@Autowired
to inject dependency
6.Annotation - Field Injection
- apply directly to a field
- no need setter method for the field
- multiple Services implement same Interface
- decide which one to inject via
Qualifier
- Cavet:
the special case of when BOTH the first and second characters of the class name are upper case, then the name is NOT converted. RESTFortuneService --> RESTFortuneService Behind the scenes, Spring uses the Java Beans Introspector to generate the default bean name. Here's a screenshot of the documentation for the key method. Java Beans Introspector
8.Annotation - Literal Value Injection
- set up
context:property-placeholder
inapplicationContext.xml
@Value(${})
9.Annotation - Bean Scope - Singleton
- default bean scope is singleton
- same as
@Scope("singleton")
10.Annotation - Bean Scope - Prototype
- Spring Container returns different beans
11.Annotation - Bean Life Cycle Methods
@PostConstruct
as init-method@PreDestroy
as destroy-method- remember prototype bean won't invoke destroy-method, b/c bean doesn't record prototype beans
- javax.annotation in not default included in JAVA 9
NOTICE if got errors when using
@PostConstruct
and@PreDestroy
Java >= 9, javax.annotation has been removed from its default classpath. Download the javax.annotation-api-1.2.jar and add it to your project as for the Spring jar files
12.Java Source Code Configuration
- Create a Java Class and annotate as
@Configuration
- Add component scanning support:
@ComponentScan
(optional) - Read Spring Java configuration class
- Retrieve bean from Spring container
13.Java Code Config - Inner Defined Bean
- define bean inside the Java code configuration
14.Java Code Config - Load Properties
- Create Properties File
- Load Properties File in Spring Config
- Reference values from Properties File
1.Set up
- New - Dynamic Web Project
- manually paste lastest Spring libraries to
WebContent/WEB-INF/lib
- manually paste additional libraries to
WebContent/WEB-INF/lib
- commons-logging-1.2.jar
- javax.servlet.jsp.jtl-1.2.1.jar
- javax.servlet.jsp.jstl-api-1.2.1.jar
- add
javax.servlet.http.HttpServlet
by Project Properties - Java Build Path - Libraries - Add Library... - Server Runtime - Tomcat
- xml configuration files in
WebContent/WEB-INF
- spring-mvc-demo-servlet.xml
- web.xml
configure the Spring Dispatcher Servlet using all Java Code (no xml): https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-container-config
2.Spring MVC - Controller & View
- annotate class with
@Controller
@Controller
inherits from@Component
... supports scanning(according to base-package setting)@Requestmapping("/")
maps a path to a method name- the method should return view name which will be assembled with prefix suffix in config
- finnaly,
Run as Server
How to Clean Server Cache: stop the tomcat in Server tab, right click the server and select
Clean...
, right click the server again and selectClean Tomcat Work Directory...
How to Clean Eclipse Cache: select the top-level menu option, Project -> Clean ..., be sure your project is selected and click OK, restart Eclipse
3.Spring MVC - Handle Form Data
- a tag as hyperlink
- HTML form action
- get param from query string
4.Spring MVC - Add Attribute to Model
model.addAttribute("foo", obj/string/...)
in controller method${foo}
in view jsp file
- config assets resource in Spring
- Deploy app to Tomcat as a Web Application Archive(WAR) file
- stop tomcat in Eclipse
- Right Click project, and select Export > WAR file
- input a file name, 'myapp.war' for example
- start tomcat outside of Eclipse
- move the new WAR file to /path/install/tomcat/webapps/
- visit
http://localhost:8080/myapp
5.Spring MVC - Annotation RequestParam
@RequestParam("foo") String bar
parse thefoo
value in queryString and evalute it tobar
6.Spring MVC - Annotation Request Parent Mapping
- Annotate the Controller with
@RequestMapping
gives a route scope, resolving ambiguous mapping
- Official Doc
- visit
localhost:8080/spring-mvc-demo/student/showForm
8.Spring MVC - Form Select Tag
- hard code options in JSP
9.Spring MVC - Form Options Tag
- read data from model layer
- use it in view layer
10.Spring MVC - Literal Value Injection
- set up
context.xml
header util:properties
11.Spring MVC - Form Radiobutton Tag
12.Spring MVC - Form Checkbox Tag
set up our development environment
- download hibernate-validation JAR from
http://hibernate.org/validator/releases/6.0/
- copy dist/xxx.jar and dist/lib/required/xxx.jar to /WEB-INF/lib
- Add validation rule to Customer Class
- Display error messages on HTML form
- Perform validation in the Controller class
- Update confirmation page
1.1.Pre-processing with @InitBinder
- treat the input only contains whitespaces as invalid
2.validate number range: min, max
3.validate using regular expression
- Convertion Type Issue Int -> String
- Use
Integer
Type instead ofInt
5.handle string input for interger field
- Override error code with
messages.properties
file
- new package
com.luv2code.springdemo.validation
- new annotation
CourseCode
- Contraint
- Target
- Retention
- new
CourseCodeConstraintValidator
implementsConstraintValidator
install (remember the temporary password)
export PATH=$PATH:/usr/local/mysql/bin
sudo /usr/local/mysql/support-files/mysql.server start
mysql -u root -p (input the temporary password)
set password = password('123456');
- Database Table
run the following sql scripts
- /scripts/01-create-user.sql
- /scripts/02-student-tracker.sql
copy /lib/required/xxx.jar to /hibernate-tutorial/lib
- download platform independent zip file
- copy /mysql-connector-java-x.y.z.jar to /hibernate-tutorial/lib
- Add JARs
For Java >=9, download the following jar files, since java.xml.bind has been removed
Project Properties - Java Build Path - Libraries - Add JARs
- Add hibernate configuration file
- src/hibernate.cfg.xml
- Annotate Java class
- @Entity
- @Table(name="tbl")
- @Id
- @Column(name="column")
- Develop Java code to perform database operations
- SessionFactory
- Read Object via Hibernate
- Query Object via Hibernate
- Update Object via Hibernate
- Delete Object via Hibernate
- run sql hb-01-one-to-one-uni/create-db.sql
- uni-directional demo
- run sql hb-02-one-to-one-bi/create-db.sql
- bi-directional demo
- error handling
- cascade delete
- no cascade delete
- run sql hb-03-one-to-many/create-db.sql
- create instructor demo
- create courses demo
- retrieve instructor courses demo
- delete course demo
- eager-demo
- lazy-demo
- lazy-demo-issue
- close session before lazy fetch
- HQL Join Fetch
- use JOIN FETCH to load all data, negating with LAZY fetch
- run sql hb-04-one-to-many-uni/create-db.sql
- create course and reviews demo
- retrieve course and reviews demo
- delete course and reviews demo
- run sql hb-05-many-to-many/create-db.sql
- configuration
- create demo
- further create demo
- retrieve data demo
- delete demo