Giter Site home page Giter Site logo

rarshion / containerx Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bootsrc/containerx

0.0 2.0 0.0 89 KB

containerx是一个迷你型IoC依赖注入框架,参考了Spring源码

Home Page: http://www.flylib.org/article/containerx

License: MIT License

Java 100.00%

containerx's Introduction

containerx

简介

containerx是一款迷你型的bean容器,IoC框架。是作者学习了Spring源码后,为了进一步学习Spring原理而自行开发的框架。
A mini IoC (dependency injection) framework 极少使用其他的第三方jar包。

用途

主要用于学习Spring/AOP原理, 或者在自己项目中用于轻量级IoC。

开发者

刘少明(Frank Liu)     git https://github.com/flylib     邮箱 [email protected]

flylib博客:         http://www.flylib.org

实现的功能

  1. setter注入(目前基于xml配置bean,未来会支持注解配置bean);
  2. 单例bean的注册和获取;
  3. AOP面向切面编程(基于xml配置);

quick start-快速上手

步骤如下:
Step 1: 在自己的项目里添加containerx的依赖

<dependency>
	<groupId>io.github.flylib</groupId>
	<artifactId>containerx</artifactId>
	<version>1.0.1</version>
</dependency>

Step2: 在自己的项目里面执行

mvn compile

Step3: 打包自己的程序

如果需要打包成可以运行的Java Application,需要注意
pom.xml增加

<properties>
	<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	<jdk.version>1.8</jdk.version>
</properties>

<build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>2.3.2</version>
			<configuration>
				<source>${jdk.version}</source>
				<target>${jdk.version}</target>
				<encoding>${project.build.sourceEncoding}</encoding>
			</configuration>
		</plugin>

		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-jar-plugin</artifactId>
			<version>2.6</version>
			<configuration>
				<archive>
					<manifest>
						<addClasspath>true</addClasspath>
						<classpathPrefix>lib/</classpathPrefix>
						<mainClass>io.github.flylib.containerx.demo.app.ContainerxDemoApp</mainClass>
					</manifest>
				</archive>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-dependency-plugin</artifactId>
			<version>2.10</version>
			<executions>
				<execution>
					<id>copy-dependencies</id>
					<phase>package</phase>
					<goals>
						<goal>copy-dependencies</goal>
					</goals>
					<configuration>
						<outputDirectory>${project.build.directory}/lib</outputDirectory>
					</configuration>
				</execution>
			</executions>
		</plugin>

	</plugins>
</build>

然后执行mvn package
在target路径下生成了containerx-demo-1.0.1.jar(包含有Main-Class和Class-Path的MANIFEST.MF)以及包含有所有依赖的jar的lib文件夹

Step4.运行自己的jar包

java -jar containerx-demo-1.0.1.jar

可以参考containerx-demo项目。

示例

containerx-demo项目 Java代码如下:

package io.github.flylib.containerx.demo.app;

import io.github.flylib.containerx.beans.factory.BeanFactory;
import io.github.flylib.containerx.beans.factory.XmlBeanFactory;
import io.github.flylib.containerx.demo.model.Person;
import io.github.flylib.containerx.demo.service.DemoService;

/**
 * containerx的demo
 * @author Frank Liu([email protected])
 *
 */
public class Main {
	public static void main(String[] args) {
		iocDemo();
	}
	
	private static void iocDemo() {
		BeanFactory beanFactory = new XmlBeanFactory("beans.xml");
		Person person = (Person)beanFactory.getBean("myPerson");
		
		System.out.println("name=" + person.getName());
		System.out.println("address=" + person.getAddress());
		
		Person person1 = (Person)beanFactory.getBean("myPerson");
		System.out.println("person=" + person);
		System.out.println("person1=" + person1);
		System.out.println("person == person1? " + (person == person1));
		
		aopDemo(beanFactory);
	}
	
	private static void aopDemo(BeanFactory beanFactory) {
		DemoService demoService = (DemoService)beanFactory.getBean("demoService");
		System.out.println("\n\n---AOP demo---");
		demoService.doSomething();
	}
}

运行结果如下:

--> DefaultDocumentLoader-getAopConfig
<?xml version="1.0" encoding="UTF-8"?><aop:aspect-list>
		<aop:aspect bean="demoAspect" id="aop1" target="demoService">
			<before>beforeMethod</before>
			<after>afterMethod</after>
		</aop:aspect>
	</aop:aspect-list>
Security framework of XStream not initialized, XStream is probably vulnerable.
id=aop1, bean=demoAspect, target=demoService
before=beforeMethod, after=afterMethod
鐢ㄤ唬鐞哹ean鍘讳唬鏇縝ean瀹瑰櫒涓師鏈夌殑bean 瀹屾瘯.
name=Frank Liu
address=Shanghai,China(**-上海)
person=io.github.flylib.containerx.demo.model.Person@15ca7889
person1=io.github.flylib.containerx.demo.model.Person@15ca7889
person == person1? true


---AOP demo---
---aop log- before---
---DemoServiceImpl.doSomething() done---
---aop log- after---

框架原理

核心方法是inject(Object bean, Map<String, Object> properties)
而其中核心的一行代码是

method.invoke(bean, methodMap.get(methodName));

常见问题的解决方案

  1. 我自己的控制台程序Java Application程序为何mvn package后提示找不到main class ?

答:在pom.xml文件中没有做项目的打包设置的时候,默认没有设置main()函数所在的class, 并且带上依赖的jar包。 对于maven的Java Application项目(控制台程序),可以字在pom.xml中增加如下设置

<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-jar-plugin</artifactId>
	<version>2.6</version>
	<configuration>
		<archive>
			<manifest>
				<addClasspath>true</addClasspath>
				<classpathPrefix>lib/</classpathPrefix>
				<mainClass>io.github.flylib.containerx.demo.app.ContainerxDemoApp</mainClass>
			</manifest>
		</archive>
	</configuration>
</plugin>
<plugin>
	<groupId>org.apache.maven.plugins</groupId>
	<artifactId>maven-dependency-plugin</artifactId>
	<version>2.10</version>
	<executions>
		<execution>
			<id>copy-dependencies</id>
			<phase>package</phase>
			<goals>
				<goal>copy-dependencies</goal>
			</goals>
			<configuration>
				<outputDirectory>${project.build.directory}/lib</outputDirectory>
			</configuration>
		</execution>
	</executions>
</plugin>

打包的时候执行

mvn package

containerx's People

Contributors

bootsrc avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.