Spring4學習(三):註解配置bean
一、註解基礎
1、元件掃描(component scanning)
Spring能夠從classpath下自動掃描,偵測和例項化具有特定註解的元件
2、特定元件包括
- @Component 基本註解,標示一個受spring管理的元件
- @Respository 標識持久層元件
- @Service 標識服務層(業務層)元件
- @Controller 標識表現層元件
3、spring配置檔案中宣告<context:component-scan>
- base-package屬性指定一個需要掃描的基類包,Spring容器將會掃描這個基類包及其子包中的所有類
- 當需要掃描多個包時,可用使用逗號分隔
如果僅希望掃描特定的類而非基包下的所有類,可用使用resource-pattern屬性過濾特定的類,例如
<context:component-scan
base-package="io.spring.beans"
resource-pattern="autowire/*.class" />
- <context:include-filter>子節點表示要包含的目標類
- <context:exclude-filter>子節點表示要排除在外的目標類
- <context:component-scan>下可以擁有若干<context:include-filter>和<context:exclude-filter>子節點
二、建立例項
1、首先引入 spring-aop-4.2.5.RELEASE.jar
2、新建io.spring.annotation包,建立TestObject.java 和 Main.java
package io.spring.annotation; import org.springframework.stereotype.Component; /** * @author 胖胖のALEX E-mail:[email protected] * @date 2016年4月7日 下午11:03:29 * @version 1.0 */ @Component public class TestObject { }
Main.java
package io.spring.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import io.spring.annotation.controller.UserController;
import io.spring.annotation.repository.UserRepository;
import io.spring.annotation.service.UserService;
/**
* @author 胖胖のALEX E-mail:[email protected]
* @date 2016年4月7日 下午11:10:34
* @version 1.0
*/
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
TestObject to = (TestObject) ctx.getBean("testObject");
System.out.println(to);
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);
}
}
3、新建io.spring.repository包,建立檔案UserRepository.java、UserRepositoryImpl.java
package io.spring.annotation.repository;
/**
* @author 胖胖のALEX E-mail:[email protected]
* @date 2016年4月7日 下午11:04:47
* @version 1.0
*/
public interface UserRepository {
public void save();
}
UserRepositoryImpl.java
package io.spring.annotation.repository;
import org.springframework.stereotype.Repository;
/**
* @author 胖胖のALEX E-mail:[email protected]
* @date 2016年4月7日 下午11:05:40
* @version 1.0
*/
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {
@Override
public void save() {
System.out.println("UserRepository Save...");
}
}
4、新建io.spring.service包,新建UserService.java
package io.spring.annotation.service;
import org.springframework.stereotype.Service;
/**
* @author 胖胖のALEX E-mail:[email protected]
* @date 2016年4月7日 下午11:06:33
* @version 1.0
*/
@Service
public class UserService {
public void add() {
System.out.println("UserService add...");
}
}
5、新建io.spring.controller包,新建UserController.java
package io.spring.annotation.controller;
import org.springframework.stereotype.Controller;
/**
* @author 胖胖のALEX E-mail:[email protected]
* @date 2016年4月7日 下午11:07:17
* @version 1.0
*/
@Controller
public class UserController {
public void execute() {
System.out.println("UserController execute...");
}
}
6、執行Main.java,正確結果如下:
三、元件裝配 @AutoWire @Resource
使用@AutoWire修改上面的例子
1、io.spring.annotation.repository
package io.spring.annotation.repository;
/**
* @author 胖胖のALEX E-mail:[email protected]
* @date 2016年4月7日 下午11:04:47
* @version 1.0
*/
public interface UserRepository {
public void save();
}
package io.spring.annotation.repository;
import org.springframework.stereotype.Repository;
/**
* @author 胖胖のALEX E-mail:[email protected]
* @date 2016年4月7日 下午11:05:40
* @version 1.0
*/
@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {
@Override
public void save() {
System.out.println("UserRepository Save...");
}
}
2、io.spring.annotation.service
package io.spring.annotation.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import io.spring.annotation.repository.UserRepository;
/**
* @author 胖胖のALEX E-mail:[email protected]
* @date 2016年4月7日 下午11:06:33
* @version 1.0
*/
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
public void add() {
System.out.println("UserService add...");
userRepository.save();
}
}
3、io.spring.annotation.controller
package io.spring.annotation.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import io.spring.annotation.service.UserService;
/**
* @author 胖胖のALEX E-mail:[email protected]
* @date 2016年4月7日 下午11:07:17
* @version 1.0
*/
@Controller
public class UserController {
@Autowired
private UserService userService;
public void execute() {
System.out.println("UserController execute...");
userService.add();
}
}
4、io.spring.annotation
package io.spring.annotation;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import io.spring.annotation.controller.UserController;
import io.spring.annotation.repository.UserRepository;
import io.spring.annotation.service.UserService;
/**
* @author 胖胖のALEX E-mail:[email protected]
* @date 2016年4月7日 下午11:10:34
* @version 1.0
*/
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
//
// TestObject to = (TestObject) ctx.getBean("testObject");
// System.out.println(to);
//
UserController userController = (UserController) ctx.getBean("userController");
System.out.println(userController);
userController.execute();
//
// UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
// System.out.println(userRepository);
//
// UserService userService = (UserService) ctx.getBean("userService");
// System.out.println(userService);
}
}
輸入結果: