spring的基礎應用(1)——定義bean
阿新 • • 發佈:2022-01-05
一、命名一個Bean
1、用id和class定義一個bean
基於xml建立一個spring的bean
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="order" class="com.ali.testspring.Order"> </bean> </beans>
其中id是一個字串,用於表示bean,是唯一的,class是類的全限定名。
在建立一個services.xml和daos.xml
services.xml
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- services --> <bean id="service" class="com.ali.testspring.Service"> <property name="dao1" ref="dao1"/> <property name="dao2" ref="dao2"/> <!-- additional collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions for services go here --> </beans>
daos.xml
<?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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="dao1" class="com.ali.testspring.Dao1"> <!-- additional collaborators and configuration for this bean go here --> </bean> <bean id="dao2" class="com.ali.testspring.Dao2"> <!-- additional collaborators and configuration for this bean go here --> </bean> <!-- more bean definitions for data access objects go here --> </beans>
在測試類裡:
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
System.out.println(context.getBean("dao1"));
System.out.println(context.getBean("dao2"));
System.out.println(context.getBean("service"));
我們發現,雖然這三個bean不在一個xml裡面,但是service仍然是可以引用dao1和dao2的。
2、用factory-method定義一個bean
看第一個示例,在ClientService中定義了一個靜態工廠方法,返回一個clientService物件。
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="clientService" class="com.ali.testspring.ClientService" factory-method="createInstance">
</bean>
</beans>
public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {}
public static ClientService createInstance() {
return clientService;
}
}
第二個示例:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userService" class="com.ali.testspring.UserService">
<!-- inject any dependencies required by this locator bean -->
</bean>
<!-- the bean to be created via the factory bean -->
<bean id="userDao"
factory-bean="userService"
factory-method="createUserDao"/>
</beans>
public class UserService {
private static UserDao userDao=new UserDao();
private UserDao createUserDao(){
return userDao;
}
}