Spring学習ノート11---注釈でbeanを配置

4136 ワード

<?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.xsd">

    <!--
     //Ioc 
    <context:component-scan base-package="annotation"></context:component-scan>
     -->
    <!--
     //resource-pattern : 
    <context:component-scan base-package="annotation" resource-pattern="service//*.class"></context:component-scan>
    -->

    <!--
    include-filter , use-default-filters="false"
    exclude-filter 
     :
    type="annotation" expression="org.springframework.stereotype.Repository", Annotation 
    type="assignable" expression="org.springframework.stereotype.UserRepository", 

    use-default-filters="false" filter, context:include-filter expresssion
    -->
    <!--
Filter        Type Examples             Expression    Description
annotation org.example.SomeAnnotation  SomeAnnoation target class
assignable org.example.SomeClass       class interface 
aspectj    org.example..*Service+          AspectJ 
regex  org\.example\.Default.*             Regelar Expression
custom org.example.MyTypeFilter        Spring3 Type, org.springframework.core.type.TypeFilter
    -->

    <!--
    <context:component-scan base-package="annotation" use-default-filters="false">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"></context:exclude-filter>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"></context:include-filter>
        <context:exclude-filter type="assignable" expression="org.springframework.stereotype.UserRepository"></context:exclude-filter>
    </context:component-scan>
     -->
</beans>
import annotation.controller.UserController;
import annotation.repository.UserRepository;
import annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("annotation//annotation.xml");
        TestObject testObject = (TestObject) ctx.getBean("testObject");
        System.out.println(testObject);

        UserController userController = (UserController) ctx.getBean("userController");
        System.out.println(userController);

        UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
        System.out.println(userRepository);

        UserService userService = (UserService) ctx.getBean("userService");
        System.out.println(userService);
    }
}
package annotation.controller;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {
    public void execute() {
        System.out.println("user contorller execute ....");
    }
}
import org.springframework.stereotype.Repository;

@Repository(value = "userRepository")
public class UserRepositoryImpl implements UserRepository {
    @Override
    public void save() {
        System.out.println("Use Repository Impl save....");

    }
}
package annotation.service;

import org.springframework.stereotype.Service;

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