黑马程序员技术交流社区
标题:
【广州校区】+【原创】模拟Spring生成对象
[打印本页]
作者:
Mylo
时间:
2018-9-10 18:54
标题:
【广州校区】+【原创】模拟Spring生成对象
本帖最后由 Mylo 于 2018-9-10 19:16 编辑
这部分只是简单模拟Spring生成对象,例如spring -> context 扫描生成对象 生成指定包下面含有指定注解的类的对象 ,只包含部分功能,后续完善
自定义注解
@Target
(ElementType.
TYPE
)
@Documented
@Retention
(RetentionPolicy.
RUNTIME
)
public
@
interface
MyCompoment
{
String
value
()
default
""
;
}
模拟的代码
package
test.factory
;
import
org.dom4j.Attribute
;
import
org.dom4j.Document
;
import
org.dom4j.DocumentException
;
import
org.dom4j.Element
;
import
org.dom4j.io.SAXReader
;
import
test.anno.
MyCompoment
;
import
test.anno.
MyController
;
import
test.anno.
MyDao
;
import
test.anno.
MyService
;
import
java.io.File
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.lang.reflect.Modifier
;
import
java.net.URL
;
import
java.util.*
;
/**
*
@program:
workSet
*
@description:
test
*
@author:
Mylo
*
@create:
2018-08-31 18:57
**/
public class
Analyze {
//标签名 ---标签的内容 -> 属性名 属性的内容
private static
Map<String
,
ArrayList<HashMap<String
,
String>>>
maps
=
new
HashMap<>()
;
private static
Map<String
,
Object>
beanMaps
=
new
HashMap<>()
;
static
{
InputStream is = Analyze.
class
.getResourceAsStream(
"/my.xml"
)
;
SAXReader reader =
new
SAXReader()
;
try
{
Document document = reader.read(is)
;
Element root = document.getRootElement()
;
List<Element> elements = root.elements()
;
/** 解析
TODO XML*/
for
(Element element : elements) {
//判断当前的maps里面是否已经有这个标签
boolean
flag =
true;
ArrayList<HashMap<String
,
String>> values =
maps
.get(element.getName())
;
if
(values ==
null
){
flag =
false;
values =
new
ArrayList<>()
;
}
//添加屬性
List<Attribute> attributes = element.attributes()
;
if
(attributes !=
null
&& attributes.size() >
0
){
HashMap<String
,
String> keysAndAalues =
new
HashMap<>()
;
for
(Attribute attribute : attributes) {
keysAndAalues.put(attribute.getName()
,
attribute.getValue())
;
}
values.add(keysAndAalues)
;
}
if
(!flag){
maps
.put(element.getName()
,
values)
;
}
}
/** 解析
TODO XML*/
Set<String> tagsName =
maps
.keySet()
;
for
(String s : tagsName) {
dom4j
(s)
;
}
}
catch
(DocumentException e) {
e.printStackTrace()
;
}
}
public static void
main
(String[] args) {
System.
out
.println(
beanMaps
.get(
"student"
))
;
}
private static void
dom4j
(String tag) {
if
(tag.equals(
"context"
)){
//根据当前的标签获取对应的值
ArrayList<HashMap<String
,
String>> context =
maps
.get(tag)
;
for
(HashMap<String
,
String> values : context) {
Set<Map.Entry<String
,
String>> entries = values.entrySet()
;
for
(Map.Entry<String
,
String> entry : entries) {
//判断功能
if
(entry.getKey().equalsIgnoreCase(
"packageToScan"
)){
String value = entry.getValue()
;
List<String> packages =
new
ArrayList<>()
;
if
(value.contains(
","
)){
String[] packageNames = value.split(
","
)
;
packages = Arrays.
asList
(packageNames)
;
}
else
{
packages.add(value)
;
}
// System.out.println(packages);
creatBeanByPaht
(packages)
;
}
}
}
}
if
(tag.equals(
"bean"
)){
ArrayList<HashMap<String
,
String>> beans =
maps
.get(tag)
;
for
(HashMap<String
,
String> bean : beans) {
Set<Map.Entry<String
,
String>> es = bean.entrySet()
;
String className =
""
;
String classPath =
""
;
for
(Map.Entry<String
,
String> entry : es) {
if
(entry.getKey().equals(
"id"
)){
className = entry.getValue()
;
}
if
(entry.getKey().equals(
"class"
)){
classPath = entry.getValue()
;
}
}
try
{
// System.out.println(classPath);
Class<?> clazz = Class.
forName
(classPath)
;
// System.out.println(classPath + "--" + clazz);
Object obj = clazz.newInstance()
;
beanMaps
.put(className
,
obj)
;
}
catch
(Exception e) {
e.printStackTrace()
;
}
}
}
}
//根据路径生成对象
private static void
creatBeanByPaht
(List<String> paths){
//beanMaps
for
(String path : paths) {
try
{
path = path.replace(
"."
,
"/"
)
;
Enumeration<URL> resources = Analyze.
class
.getClassLoader().getResources(path)
;
path = path.replace(
"/"
,
"
\\
"
)
;
while
(resources.hasMoreElements()){
String fileRealPath = resources.nextElement().getPath()
;
File file =
new
File(fileRealPath)
;
if
(file!=
null
&& file.listFiles().
length
>
0
){
File[] files = file.listFiles()
;
for
(File tempFile : files) {
if
(tempFile.isFile() && tempFile.getName().contains(
".class"
)){
String tempFilePath = tempFile.getAbsolutePath().substring(tempFile.getAbsolutePath().indexOf(path))
;
int
dropIndex = tempFilePath.indexOf(
"."
)
;
String fileName = tempFilePath.substring(
0
,
dropIndex).replace(
"
\\
"
,
"."
)
;
try
{
Class<?> clazz = Class.
forName
(fileName)
;
//判断这个类不是一个接口 或者抽象类 如果不是就 生成对象
if
(!Modifier.
isInterface
(clazz.getModifiers()) && !Modifier.
isAbstract
(clazz.getModifiers())){
if
(clazz.isAnnotationPresent(
MyCompoment
.
class
) || clazz.isAnnotationPresent(
MyController
.
class
) || clazz.isAnnotationPresent(
MyService
.
class
) || clazz.isAnnotationPresent(
MyDao
.
class
)){
String className =
""
;
MyCompoment
myCompoment = clazz.getAnnotation(
MyCompoment
.
class
)
;
if
(myCompoment !=
null
){
className = myCompoment.value()
;
}
MyController
myController = clazz.getAnnotation(
MyController
.
class
)
;
if
(myController !=
null
){
className = myController.value()
;
}
MyService
myService = clazz.getAnnotation(
MyService
.
class
)
;
if
(myService !=
null
){
className = myService.value()
;
}
MyDao
myDao = clazz.getAnnotation(
MyDao
.
class
)
;
if
(myDao !=
null
){
className = myDao.value()
;
}
//如果没有设置类名 就默认使用类名 首字母小写
if
(className.equals(
""
)){
className = clazz.getName().substring(clazz.getName().lastIndexOf(
"."
) +
1
)
;
className = className.substring(
0
,
1
).toLowerCase() + className.substring(
1
)
;
}
//创建对象
Object obj = clazz.newInstance()
;
beanMaps
.put(className
,
obj)
;
}
}
}
catch
(Exception e) {
e.printStackTrace()
;
}
}
}
}
}
}
catch
(IOException e) {
e.printStackTrace()
;
}
}
}
}
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2