原型模式是一种创建型设计模式,它允许我们通过复制现有对象来创建新对象,而不需要重新实例化和初始化。在Spring和Shiro中,原型模式经常用于创建和配置对象,从而提高代码的可复用性和可维护性。
Spring中的PrototypeBeanFactory类和Shiro中的SessionDAO类都是原型模式的应用。下面我们将分别介绍这两个类的应用。
- Spring中的PrototypeBeanFactory
PrototypeBeanFactory是Spring Framework中的一个类,它允许我们以编程方式创建和配置原型Bean对象,从而创建和配置Spring Bean。下面是一个简单的示例,演示如何使用PrototypeBeanFactory创建一个包含两个属性的原型Spring Bean:
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class PrototypeBeanFactoryExample {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) context.getBeanFactory();
GenericBeanDefinition prototypeBeanDefinition = new GenericBeanDefinition();
prototypeBeanDefinition.setBeanClass(User.class);
prototypeBeanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
prototypeBeanDefinition.getPropertyValues().add("name", "John");
prototypeBeanDefinition.getPropertyValues().add("age", 30);
beanFactory.registerBeanDefinition("user", prototypeBeanDefinition);
User user1 = context.getBean("user", User.class);
User user2 = context.getBean("user", User.class);
System.out.println(user1);
System.out.println(user2);
}
}
class User {
private String name;
private int age;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
在这个示例中,我们使用PrototypeBeanFactory创建了一个User对象,并将其注册为原型Spring Bean。我们可以使用getPropertyValues()方法为User对象设置属性值,并使用getBean()方法获取User对象。由于我们将User对象注册为原型Bean,因此每次获取Bean对象时都会创建一个新的实例。
- Shiro中的SessionDAO
SessionDAO是Shiro中的一个接口,它定义了一组用于管理Session的方法。在Shiro中,我们可以使用SessionDAO的实现类来创建和配置Session对象。下面是一个简单的示例,演示如何使用SessionDAO创建和配置Session对象:
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.DefaultSessionManager;
import org.apache.shiro.session.mgt.SessionFactory;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.apache.shiro.session.mgt.eis.CachingSessionDAO;
import java.io.Serializable;
import java.util.UUID;
public class SessionDAOExample {
public static void main(String[] args) {
SessionDAO sessionDAO = new CachingSessionDAO();
DefaultSessionManager sessionManager = new DefaultSessionManager();
sessionManager.setSessionDAO(sessionDAO);
sessionManager.setSessionFactory(new SessionFactory() {
@Override
public Session createSession(SessionContext sessionContext) {
return new SimpleSession();
}
});
Session session1 = sessionManager.start(new SimpleSession());
Session session2 = sessionManager.start(new SimpleSession());
System.out.println(session1.getId());
System.out.println(session2.getId());
}
}
class SimpleSession implements Session {
private Serializable id;
public SimpleSession() {
this.id = UUID.randomUUID().toString();
}
@Override
public Serializable getId() {
return id;
}
@Override
public Date getStartTimestamp() {
return null;
}
@Override
public Date getLastAccessTime() {
return null;
}
@Override
public long getTimeout() throws InvalidSessionException {
return 0;
}
@Override
public void setTimeout(long l) throws InvalidSessionException {
}
@Override
public String getHost() {
return null;
}
@Override
public void touch() throws InvalidSessionException {
}
@Override
public void stop() throws InvalidSessionException {
}
@Override
public Collection<Object> getAttributeKeys() throws InvalidSessionException {
return null;
}
@Override
public Object getAttribute(Object o) throws InvalidSessionException {
return null;
}
@Override
public void setAttribute(Object o, Object o1) throws InvalidSessionException {
}
@Override
public Object removeAttribute(Object o) throws InvalidSessionException {
return null;
}
}
在这个示例中,我们使用CachingSessionDAO创建了一个Session对象,并使用DefaultSessionManager管理Session对象。我们可以使用setSessionFactory()方法设置SessionFactory对象,并使用start()方法创建Session对象。由于我们使用的是SimpleSession类实现了Session接口,因此在创建Session对象时会调用SimpleSession的构造函数。每次创建Session对象时都会生成一个新的UUID作为Session的ID。
以上就是基于Spring和Shiro源码的原型模式的简单应用示例。原型模式可以帮助我们更好地组织和管理代码,提高代码的可复用性和可维护性。