spring05

通过静态工厂的方法创建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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 通过静态工厂的方法来配置bean ,注意不是配置静态工厂的实例,而是配置bean的实例 -->
    <!-- class 属性:指向的是静态工厂方法的全类名
       factory-method :指向的是静态工厂方法的名字
       constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg来配置参数 -->
       <bean id="car" class="lib2.Car"
       factory-method="gerCae">
       <constructor-arg value="audi"></constructor-arg></bean>
       <!-- 配置工厂的实例 -->
       <bean id="casec" class="instance"></bean>
       <!-- 通过实例工厂方法来配置bean -->
           <!-- factory-bean: 属性:指向的是静态工厂方法的bean 
       factory-method :指向的是静态工厂方法的名字
       constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg来配置参数 -->
       <bean id="cae" factory-bean="cad" factory-method="cac">
       <constructor-arg value="asca"></constructor-arg></bean>

</beans>

factory来配置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 http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 
    通过factorybean来配置bean的实例
    class:只想factorybean的全类名
    property:配置的事factorybean的属性(可能是里面的复制函数set) -->
    <bean id="Car" class="factorybean">
    <property name="brand" value="bwm"></property></bean>

</beans>

基于注解来配置bean和bean的线相关的属性。

 组件扫描:spring能够从classpath夏自动扫描、侦测和实例化具有特定注释的组件;

 

 

 

package test1;

import org.springframework.stereotype.Component;

@Component
public class TestObject {

}
package test2;

import org.springframework.stereotype.Repository;

@Repository
public class UserReposityImpl implements UserRepository {

    @Override
    public void save() {
        // TODO Auto-generated method stub
        System.out.println("UserRepository save....");

    }
}package test3;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    public void add()
    {
        System.out.println("UserSerivce add..");
    }

}
package test4;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {
public void execute()
{
    System.out.println("UserController execute..");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
    <!-- 指定spring ioc容器扫描的包 -->   
    <context:component-scan base-package="test1.TestObject"></context:component-scan>

</beans>
package test1;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import test2.UserRepository;
import test3.UserService;
import test4.UserController;

public class Main {
public static void main(String[] args) {
    // TODO Auto-generated method stub
    ApplicationContext ct=new ClassPathXmlApplicationContext("beans-053.xml");
    TestObject to=(TestObject) ct.getBean("testObject");
    UserController userController=(UserController) ct.getBean("userController");
    UserRepository userRepository=(UserRepository) ct.getBean("userRepository");
    UserService userService=(UserService) ct.getBean("userService");
    System.out.println(to);
    System.out.println(userController);
    System.out.println(userService);
    System.out.println(userRepository);
    
}
}

 

 

 

关于MyBeanPostProcessor的应用。

 

posted @ 2020-04-02 22:13  喜欢爬的孩子  阅读(179)  评论(0编辑  收藏  举报