初学spring,还是感觉东西有点杂的,难度不大就是有点碎,还是记录一下吧。
1. 依赖注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""xmlns:xsi=""xmlns:context=""xmlns:aop=""xsi:schemaLocation="://www.springframework/schema/beans/spring-beans.xsd://www.springframework/schema/tx/spring-tx.xsd://www.springframework/schema/context/spring-context-2.5.xsd://www.springframework/schema/aop/spring-aop.xsd">
这是先引入的命名空间, 文件命名为l。
如果我要在spring容器注册一个bean,则可以在applicationContext里加入以下代码
<bean id="user" class="com.kjy.domain.User" scope="prototype"><!--普通属性--><property name="username" value="kang"/><property name="age" value="23"/><!--list 集合属性--><property name="habits"><list><value>eat</value><value>sleep</value><value>code</value></list></property><!--map 集合属性--><property name="map"><map><entry key="1" value="A"/><entry key="2" value="B"/><entry key="3" value="C"/></map></property>
</bean>
此时相应类(com.kjy.domain.User)要有相应的setter方法(setUsername,setAge,setHabits等等),其中list和map里面的元素也可以是复杂数据类型、一些自定义的类的实例等等,标签变为相应的-ref就行。这里scope的取值是多例的意思,相应的"singleton"是单例的意思,不写就默认单例。
<bean id="person" class="com.kjy.demo.Person" scope="prototype"><constructor-arg name="username" value="sun"/><constructor-arg name="age" value="32"/>
</bean>
这是构造方法依赖注入。和setter方法相似,constructor-arg代替property,需要注意的是,这里name属性的取值是构造方法的形参的意思。
类似的,我们可以写出数据源的实例化代码:
<context:property-placeholder location="classpath:druid.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="username" value="${username}"/><property name="password" value="${password}"/><property name="driverClassName" value="${driverClassName}"/><property name="url" value="${url}"/><property name="maxActive" value="${maxActive}"/><property name="maxWait" value="${maxWait}"/><property name="initialSize" value="${initialSize}"/>
</bean>
<!--扫描指定包下所有类并筛选出特定注解的类注册为bean添加到容器中-->
<context:component-scan base-package="com.kjy"/>
然后以UserService为例:
//注册(Service代表这是服务层的类,其他可选注解有Reposity,Controller,Component等,代表不同语义)
@Service("userService")
//单例(默认)
@Scope("singleton")
public class UserServiceImpl implements UserService {//下面注解的意思是从spring容器中匹配符合筛选条件的bean注入成员变量/*@Autowired //代表按类型注入@Qualifier("userDao") //代表按名称注入*/@Resource //相当于上面两个注解的作用private UserDao userDao;@Value("value") //普通数据类型private String string;
}@Repository("userDao")
public class UserDaoImpl implements UserDao {
}
发现对于非自定义的类,这种方式似乎失效。这里可以扩展一下,对于非自定义的类,我们也可以采用注解的方式,以DruidDataSource为例:
@Configuration //表明这是配置类
@ComponentScan("com.kjy") //指定扫描包
@Import({DataSourceConfig.class}) //导入其他部分的配置类
public class SpringConfig {
}@PropertySource("classpath:druid.properties") //指定配置文件
public class DataSourceConfig {@Value("${username}")private String username;@Value("${password}")private String password;@Value("${url}")private String url;@Value("${driverClassName}")private String driverClassName;@Value("${maxActive}")private String maxActive;@Value("${maxWait}")private String maxWait;@Value("${initialSize}")private String initialSize;@Bean("dataSource")public DataSource getDataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setUsername(username);dataSource.setPassword(password);dataSource.setDriverClassName(driverClassName);dataSource.setUrl(url);dataSource.setMaxActive(Integer.valueOf(maxActive));dataSource.setMaxWait(Integer.valueOf(maxWait));dataSource.setInitialSize(Integer.valueOf(initialSize));return dataSource;}
}
2. AOP
AOP就是面向切面编程,是面向对象编程的衍生,其核心是通过动态代理的方式增强对象的某些方法。
同样,也可以分为xml方式和注解方式。
这是一个简单的例子:
<bean id="target" class="com.kjy.aop.Target"/><bean id="aspect" class="com.kjy.aop.MyAspect"/><aop:config><aop:aspect ref="aspect"><!--<aop:before method="before" pointcut="execution(public void com.kjy.hod())">--><aop:pointcut id="myPointcut" expression="execution(public void com.kjy.hod())"/><aop:before method="before" pointcut-ref="myPointcut"/></aop:aspect></aop:config>
这里,Aspect为切面类hod()方法是切点。切面类中的方法是为切点服务的(切点+切面类的方法=增强方法),实现动态代理。
除了<aop:before/>
标签以外,还有<aop:after-returnning>
,<aop:after>
等等标签,分别代表不同的添加位置。
@Component("target")
public class Target implements TargetInterface {@Overridepublic void method(){System.out.println("");}
}@Component("myAspect")
@Aspect //表示这是一个切面类
public class MyAspect {@AfterReturning("execution(public void com.kjy.hod())")public void afterReturn(){System.out.println("this is ");}@Around("execution(* com.kjy.aop.Target.*(..))")public Object around(ProceedingJoinPoint pjp) throws Throwable {System.out.println("前置增强...");Object obj = pjp.proceed();System.out.println("后置增强...");return obj;}
}
这里要注意需要在l文件加入这样一行:
<aop:aspectj-autoproxy/>
3. Junit
Junit的基本用法:
import org.junit.Test;
public class AopTest {@Testpublic void test(){ApplicationContext app = new ClassPathXmlApplicationContext(l");TargetInterface target = Bean("target");hod();}
}
发现每次写测试方法的时候都要先获取具体对象才能测试其方法,感觉有点麻烦。
此时可以采用下面这种写法:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("l")
public class AopTest {@Resource(name = "target")private TargetInterface target;@Testpublic void test(){hod();}
}
暂时写到这里吧,其实好多内容写的太粗略没有展开,还是因为我太懒了。
本文发布于:2024-02-01 03:38:16,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170672989633585.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |