XML
- First Approach
- Simpler
- Separation of Concerns
--------------------------------------------------------------
Copy Demo
改pom.xml
-------------------------------------------------------------
applicationContext.xml
- Name doesn't matter
- Spring Context sort of a HashMap
- Can simply be a registry
- XML configuration begins with this file
- Namespaces aid in configuration/validation
--------------------------------------------------------------
Application Context Demo
resources -》New -》 Spring Bean Configuration File
Namespaces
XML Declaration
Beans
- Essentially Classes
- Replaces keyword new
- Define Class, use Interface
<bean name="customerRepository" class="com.pluralsight.repository.HibernateCustomerRepositoryImpl" />
---------------------------------------------------------------------------------
Setter Injection
- Setter Injection
- Constructor Injection
- Used together
- Better for Existing Code
------------------------------------------------------------------------
Setter Injection Demo
=====》》
Source -》Sort Members
applicationContext.xml
Application.java
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean name="foo"
class="com.pluralsight.repository.HibernateCustomerRepositoryImpl" />
<bean name="customerService"
class="com.pluralsight.service.CustomerServiceImpl">
<property name="customerRepository" ref="foo"></property>
</bean>
</beans>
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.pluralsight.service.CustomerService;
import com.pluralsight.service.CustomerServiceImpl;
public class Application {
public static void main(String[] args) {
//CustomerService service = new CustomerServiceImpl();
ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
CustomerService service = appContext.getBean("customerService", CustomerService.class);
System.out.println(service.findAll().get(0).getName());
}
}
--------------------------------------------------------------------------
Constructor Injection
- Guaranteed Contract
- Constructor Defined
- Used together
- Index based
xml
class
Appliction.java
--------------------------------------------------------------------------
Autowire
- Spring Automatically Wires Beans
- byType
- byName
- constructor
- no
Autowire by Constructor
Autowire by Name and Type
By name
===============》
--------------------------------------------------------------------------
Summary
- applicationContext.xml
- Bean Definition
- Setter Injection
- Constructor Injection
- Auto wiring
----------------------------------------------------------------------
Annotation Configuration Using XML
https://blog.csdn.net/u012596785/article/details/79811114