Java - Spring 基礎學習筆記 下

Aware

Spring中定義了一系列Aware接口,可用於感知spring container,獲取container中的信息,bean的信息。
例如 ApplicationContextAware可以獲取object所在的ApplicationContext,BeanNameAware可用於獲取object所在bean的名稱。

public class Cat implements ApplicationContextAware,BeanNameAware{
	private Card card;
	private ApplicationContext context;
	
	// other methods ...
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		this.context = applicationContext;
	}
	@Override
	public void setBeanName(String name) {
		System.out.println("Bean name is " + name);
	}
}

LifeCycle Callbacks

  1. 使用 AbstractApplicationContext 註冊hook
    (1) Sound.java 中 添加
AbstractApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");
context.registerShutdownHook();

(2)Cat 實現 InitializingBean,DisposableBean

public class Cat implements InitializingBean{...}

或者 自定義函數 並在xml中添加 init-method/destroy-method 指向自定義的函數名。

// Cat.java 添加方法
public void myInit() {
	System.out.println("This is my init method");
}
// xml
<bean id="cat" class="hello.Cat" autowire="byName" init-method="myInit"></bean>

BeanPostProcessor & BeanFactoryPostProcessor

Bean實例化前後,添加自定義操作
新建Display.java

// Display.java
public class Display implements BeanPostProcessor{

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("Before Bean "+beanName+" init...");
		return null;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("Bean "+beanName+" finished...");
		return null;
	}
	<bean class="hello.Display"></bean>
}

// xml 添加
<bean class="hello.Display"></bean>

如果需要在BeanFactory 初始化前操作,使用BeanFactorPostProcessory用法與上面的類似。

Annotation 注入

@AutoWired: 按名字注入
@Qulifier(“relatedCard”) xml中找尋qulifier標籤中value是relatedCard的
@Resource(name=“card”) xml找bean id=card的注入

public class Cat{
	private Card card;
	
	public void sound() {
		System.out.println(this.card.toString()+" sound: Meow Meow Meow");
	}
	// setters and getters
	public Card getCard() {
		return card;
	}
	@Autowired
	public void setCard(Card card) {
		this.card = card;
	}
	public void myInit() {
		System.out.println("This is my init method");
	}
}
// xml
	<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"></bean>

超好用的 @Component

之前的xml中每個bean都有對應的標籤,這個實在很麻煩,spring提供了Component annotation可以標誌一個類屬於Spring Container

// Cat.java
@Component
public class Cat{
	private Card card;
	
	public void sound() {
		System.out.println(this.card.toString()+" sound: Meow Meow Meow");
	}
	// setters and getters
	public Card getCard() {
		return card;
	}
	@Autowired
	public void setCard(Card card) {
		this.card = card;
	}
	@PostConstruct
	public void myInit() {
		System.out.println("This is my init method");
	}
	@PreDestroy
	public void myDestory() {
		System.out.println("This is my destory");
	}
}

// xml
<bean id="card" class="hello.Card">
		<property name="name" value="Jack"></property>
		<property name="type" value="SmallCat"></property>
	</bean>
	<context:annotation-config></context:annotation-config>
	<context:component-scan base-package="hello"></context:component-scan>

除了@Component 之外 @Repository、@Service、@Controller 都可起到同樣的效果。由於Spring會把@Repository標誌的類封裝成Spring 數據訪問異常類型,因此只能用於Dao層。其他幾個與Component意義不同,但作用一致。
@Component 泛化,都可用
@Repository 只可Dao層
@Service 業務層,但是目前該功能與 @Component 相同
@Controller 控制層,但是目前該功能與 @Component 相同

從 properties 文件獲取信息

// mymessage.properties
greeting=Hello

//sound.java
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("SpringConfig.xml");
		String text = context.getMessage("greeting", null,"Defaule Message", null);
		System.out.println(text);
	}

// xml
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
	<property name="basenames">
		<list>
			<value>mymessages</value>
		</list>
	</property>
</bean>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章